ios 11透明导航栏

Rob*_*rga 19 uinavigationbar navbar ios swift ios11

创建一个透明的导航栏不再适用于ios 11.我在顶部得到这个黑条,因为表格视图不再出现在条形图下面(故事板中的插图设置正确,从0开始)任何想法为什么?

在此输入图像描述

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
Run Code Online (Sandbox Code Playgroud)

5pe*_*ers 16


旧:

如果您使用过tableView,请添加代码:

if (@available(iOS 11.0, *)) {
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
    self.automaticallyAdjustsScrollViewInsets = NO;
}
Run Code Online (Sandbox Code Playgroud)

新:

更改iOS11中的automaticAdjustsScrollViewInsets:

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets 
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's 
contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); 
// Defaults to YES
Run Code Online (Sandbox Code Playgroud)

关于contentInsetAdjustmentBehavior:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
    UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewContentInset = YES inside a navigation controller, regardless of whether the scroll view is scrollable
    UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
    UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
    UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));

/* Configure the behavior of adjustedContentInset.
 Default is UIScrollViewContentInsetAdjustmentAutomatic.
 */
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
Run Code Online (Sandbox Code Playgroud)

这可能是iOS11的safeArea问题.从一位专家那里尝试这个定义:

#define  adjustsScrollViewInsets_NO(scrollView,vc)\
do { \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
        if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
            [scrollView   performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\
        } else {\
            vc.automaticallyAdjustsScrollViewInsets = NO;\
        }\
    _Pragma("clang diagnostic pop") \
} while (0)
Run Code Online (Sandbox Code Playgroud)


Wuj*_*ujo 13

我遇到了同样的问题,我能够解决它.这对我有用:

public override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.backgroundColor = UIColor.clear
    self.navigationController?.navigationBar.isTranslucent = true
    if #available(iOS 11.0, *) {
        collectionView.contentInsetAdjustmentBehavior = .never
    } else {
        // Fallback on earlier versions
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一件事,我发现仍然需要让它发挥作用.最有可能的是,您将UICollectionView/UITableView/UIScrollview对齐到安全区域的顶部.将此约束更改为与超级视图的顶部对齐.

在此输入图像描述

就是这样.这不是直截了当的直观吗?谢谢Apple.


小智 9

我有一个类似的问题.我在故事板中为UIViewController设置了"Extended Edges:Under Top/Bottom/Opaque Bar". 像这样. 您也可以尝试禁用" 自动调整滚动视图插图 "

  • 我已经检查过顶部和底部,但不是不透明的.检查所有三个修复了问题. (3认同)