简体   繁体   English

ios 11 透明导航栏

[英]ios 11 transparent navigation bar

Creating a transparent navigation bar no longer works with ios 11. I get this black bar at the top because the table view doesn't come under the bar anymore (the insets in the storyboard are set properly to start from 0) Any ideas why?创建透明导航栏不再适用于 ios 11。我在顶部看到这个黑色栏,因为表格视图不再位于栏下方(故事板中的插图已正确设置为从 0 开始)任何想法为什么?

在此处输入图片说明

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true

I faced the same problem and I was able to solve it.我遇到了同样的问题,我能够解决它。 Here is what works for me:以下是对我有用的方法:

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
    }
}

And one more thing, that I found still necessary to make it working.还有一件事,我发现仍然有必要让它工作。 Most probably you have your UICollectionView/UITableView/UIScrollview aligned to top of Safe Area.很可能您将 UICollectionView/UITableView/UIScrollview 对齐到安全区域的顶部。 Change this constraint to be aligned to top of super view instead.将此约束更改为与超级视图的顶部对齐。

在此处输入图片说明

And that's it.就是这样。 Isn't it straightforward and intuitive?是不是简单直观? Thanks Apple.谢谢苹果。


old:旧:

if you have used tableView,add code:如果您使用过 tableView,请添加代码:

if (@available(iOS 11.0, *)) {
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
} else {
    self.automaticallyAdjustsScrollViewInsets = NO
}

new:新:

a change of automaticallyAdjustsScrollViewInsets in iOS11: 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

about contentInsetAdjustmentBehavior:关于 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));

it could be a problem of safeArea for iOS11.这可能是 iOS11 的安全区域问题。 try this define from one expert:试试这个专家的定义:

#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)

I had a similiar issue.我有一个类似的问题。 I set "Extended Edges: Under Top/Bottom/Opaque Bar" true for UIViewController in the storyboard.我在情节提要中为 UIViewController 设置了“扩展边缘:在顶部/底部/不透明条下”为真。 Like this.像这样。 Also you can try to disable " Automatically Adjusts Scroll View Insets "您也可以尝试禁用“ 自动调整滚动视图插入

to have consistent behavior between iOS 10 and 11, try adding this to your navigationViewController要在 iOS 10 和 11 之间保持一致的行为,请尝试将其添加到您的 navigationViewController

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

    if navigationBar.isTranslucent, #available(iOS 11.0, *) {           
        viewController.additionalSafeAreaInsets.top = -navigationBar.bounds.height
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM