您的位置:首页 > 其它

重写navigationBar返回按钮,打包安装 pushViewcontroller崩溃问题

2017-11-09 15:58 127 查看
重写navigationBar的返回按钮后,在iOS11崩溃,11以下没有问题。

这里的重写主要是 写了返回按钮文本的偏移量,大体上的代码是这样:

[[UIBarButtonItem
appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-100,
0) forBarMetrics:UIBarMetricsDefault];

总之 上面就是调用了setBackButtonTitlePositionAdjustment:forBarMetrics:来改变返回按钮文本的偏移量。在iOS11以前是没有问题的,但是iOS11现在是有问题的,不知到这是不是系统的bug。目前来说
苹果不修复,那就只能我们来做。

现在需要做的就是,在iOS11下,不要这么写

我首先想到的是 判断系统版本,iOS11及以上版本,用另外的方法替换。

例如if(@available(iOS 11, *)) {
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal]; [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];

} else {
//去掉返回按钮文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-100, 0) forBarMetrics:UIBarMetricsDefault];
}

这样就规避掉iOS11调用设置偏移量的方法了。你可以试试 iOS版本全用
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
行不行,我没有试。

这里多提两句,判断系统版本可以使用多种方法,

上面那个方法是swift的判断方法,oc也有很多方法判断,例如://判断系统版本方案
//一
if (NSFoundationVersionNumber >= NSFoundationVersionNumber10_11) {
//
}
//二
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSComparisonResult comparisonResult = [systemVersion compare:@"11.0.0" options:NSNumericSearch];

if (comparisonResult == NSOrderedAscending) {
// < iOS 11.0.0
} else if (comparisonResult == NSOrderedSame) {
// = iOS 10.0.1
} else if (comparisonResult == NSOrderedDescending) {
// > iOS 10.0.1
}

//或者

if (comparisonResult != NSOrderedAscending) {
// >= iOS 11.0.0
} else {
// < iOS 11.0.0
}
//三
double systemVersion = [UIDevice currentDevice].systemVersion.doubleValue;

if (systemVersion >= 11.0) {
// >= iOS 10.0
} else {
// < iOS 10.0
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息