我们的项目需求是,只显示图片的返回按钮,不显示上级的标题。iOS11之前通过以下方式实现:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
iOS11这种方式不行了,会导致页面错乱。
iOS11可以通过设置标题颜色透明的方式来隐藏标题:
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
通过这种方式是可以达到想要的效果的,但是这样有个问题,如果照上边这样设置了,再通过下面的方式设置左右导航文字时也会显示不出来
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
我的解决办法是:通过设置自定义button的方式来解决这个问题:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
_itemRightButton = button;
button.frame = CGRectMake(0, 0, 80, 40);
[button setTitle:@"完成" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:13];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button setTitleColor:mNormalGreenColor forState:UIControlStateNormal];
[button addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithCustomView:button];
以上就是我的解决方案,希望有更好解决方案的同学可以分享下