iOS 11导航栏返回按钮隐藏标题

对于iOS 11隐藏导航栏返回按钮标题导致返回图标向下偏移的问题,具体原因这里就不说了,网上一搜一大把对相关问题的分析,处理方式大概有以下三种:

1、遍历subViews调整布局;

2、[UIBarButtonItem appearance]设置title颜色为clearColor,副作用很明显,会导致导航栏所有buttonItem都变成透明;

3、分类自定义返回按钮;

具体情况就不一一列举了,这里提供一种更加简单的方式:

1、导入第三方库 pod 'Aspects'

2、- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions中统一配置UINavigationBar的外观,并设置返回图标

UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];

[navigationBarAppearance setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back"]];

[navigationBarAppearance setBackIndicatorImage:[UIImage imageNamed:@"nav_back"]];

//iOS 11以下隐藏返回按钮文字

if ((SYSTEM_VERSION_LESS_THAN(@"11.0"))) {

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

}

//iOS 11以上隐藏返回按钮文字

if ((SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0"))) {

 [UIViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionAfter usingBlock:^(idaspectInfo){

UIViewController *controller = aspectInfo.instance;

controller.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

} error:nil];

}

你可能感兴趣的:(iOS 11导航栏返回按钮隐藏标题)