【问题标题】:ios 11 transparent navigation barios 11 透明导航栏
【发布时间】:2018-03-05 18:12:02
【问题描述】:

创建透明导航栏不再适用于 ios 11。 我在顶部得到了这个黑色条,因为表格视图不再位于条下方(情节提要中的插图已正确设置为从 0 开始) 任何想法为什么?

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

【问题讨论】:

  • 您是否也尝试添加 self.navigationController?.navigationBar.backgroundColor = UIColor.clear ?
  • 是的,我做到了。结果相同
  • 还是一样的结果
  • 你的背景色是什么?
  • 它是黑色的。因此,您会在导航栏下方看到黑色。所以它是透明的,但它是插入的问题。表格视图不会滑入导航栏下方。在任何其他 ios 版本中都可以正常工作。

标签: ios swift uinavigationbar navbar ios11


【解决方案1】:

我有一个类似的问题。我在情节提要中为 UIViewController 设置了“扩展边缘:在顶部/底部/不透明条下”为真。 Like this. 您也可以尝试禁用“Automatically Adjusts Scroll View Insets

【讨论】:

  • 我已经检查了顶部和底部,但不是不透明的。检查所有三个解决了问题。
【解决方案2】:

旧:

如果你用过tableView,添加代码:

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

新:

iOS11中自动调整ScrollViewInsets的变化:

@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

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

这可能是 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)

【讨论】:

    【解决方案3】:

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

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

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

    就是这样。这不是简单直观吗?感谢苹果。

    【讨论】:

    • 呸!!!!感谢那!尽管在我的视图控制器的viewDidLoad 方法中添加了tableView.contentInsetAdjustmentBehavior = .never,这对我来说是iOS11Xcode 9 的问题。 Interface Builder 有时很难捕捉到这些细微的错误。非常感谢!
    • 谢谢伙计!对我帮助很大。
    • 嗨,试过了,但仍然在导航栏下看到了 collectionview
    【解决方案4】:

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

    【讨论】: