今年6月份的WWDC大会已经过去,在此会上有众多的新特性。这里强调说一下UINavigationBar
。
UINavigationBar相关
先说下效果,在用到下面两个属性时,导航条不再是64的高度,导航标题也会变大,文字靠左对齐,当上下滑动页面时,导航条的高度会进行拉伸,文字也会缩放。

- prefersLargeTitles
- largeTitleDisplayMode
用法:
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.title = @"xxxx";
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAutomatic;
自定义UINavigationBar
相较于iOS11之前的版本,UINavigationBar
结构上也与之前不同。如图:


不难看出,原来的UINavigationItemView
被替换为_UINavigationBarContentView
如果直接初始化UINavigationBar
并添加在当前ViewController上时就会出现图三所示的情况,即:子控件的高度只有44。为了解决此问题,可以先继承UINavigationBar
,然后再遍历UINavigationBar
的子控件并修改Frame:

- (void)layoutSubviews {
[super layoutSubviews];
#if TARGET_OS_IOS
for (UIView *aView in self.subviews) {
if ([@[@"_UINavigationBarBackground", @"_UIBarBackground"] containsObject:NSStringFromClass([aView class])]) {
aView.frame = CGRectMake(0, -CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)+CGRectGetMinY(self.frame));
}
}
#endif
}
效果和iOS11之前一样,当然这不是最优方案,过于简单粗暴了点。

开发环境
- Xcode_9_beta_2
网友评论