导航栏状态栏通透ios11适配

状态栏通透.png
想要实现状态栏通透的效果:
iOS10及以下设备设置控制器

1
2
3
4
5
6
7
8
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏的背景图透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// 设置导航栏底部线不显示
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
self.automaticallyAdjustsScrollViewInsets = NO; // 9.0设备上
}

在ios11的设备上,可以设置UIScrollViewcontentInsetAdjustmentBehavior的属性;

例如:设置tableViewcontentInsetAdjustmentBehavior的属性

1
2
3
4
5
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}

查看这个属性的相关信息得知,将要废除,并有新的属性替换

1
@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

因为控制器的automaticallyAdjustsScrollViewInsets默认是YES,也就是控制器的视图会自动适应导航栏和状态栏的缩进;反之不自动适应,则从屏幕顶部开始。

UIScrollViewContentInsetAdjustmentBehavior同理,设置UIScrollViewContentInsetAdjustmentNever则不自动适应,从屏幕顶部开始。

1
2
3
4
5
6
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {`
UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = 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));

导航栏通透.gif