iOS11设置返回按钮title为空

iOS11之前设置返回按钮的文字偏移来使title为空,iOS11失效了。

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

iOS11的导航栏结构有比较大的改变,可以创建一个UIView的类目加入如下代码,放入工程就可以使iOS11上返回按钮title为空。(记得导入pod 'JRSwizzle')

+ (void)load
{
    if (@available(iOS 11, *)) {
        [NSClassFromString(@"_UIBackButtonContainerView")     jr_swizzleMethod:@selector(addSubview:) withMethod:@selector(iOS11BackButtonNoTextTrick_addSubview:) error:nil];
    }
}
- (void)iOS11BackButtonNoTextTrick_addSubview:(UIView *)view
{
    view.alpha = 0;
    if ([view isKindOfClass:[UIButton class]]) {
        UIButton *button = (id)view;
        [button setTitle:@" " forState:UIControlStateNormal];
    }
    [self iOS11BackButtonNoTextTrick_addSubview:view];
    
}

以上方法不要用了,iOS11.3之后会出现BUG。Push 或 Pop Animated为YES时内存会无限暴涨直至崩溃。
可使用以下代码替代:

    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:0.1], NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];

你可能感兴趣的:(iOS11设置返回按钮title为空)