iOS返回按钮修改总结

参考
修改iOS返回按钮的几种方式
修改导航栏返回按钮的文字
iOS11更新 导航返回按钮偏移的问题

第一种

UIImage *backButtonImage = [[UIImage imageNamed:@"back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
  • 提示:上面这种方法会被系统给拉伸,需要在当前控制器里面设置

第二种

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
backItem.title = @"";
//主要是以下两个图片设置
self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"backPic"];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"backPic"];        
self.navigationItem.backBarButtonItem = backItem;
  • 提示: 这种方法在父类方法中设置好使

第三种

//重新创建一个barButtonItem
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"你想要设置的返回按钮文字" style:UIBarButtonItemStylePlain target:nil action:nil];
//设置backBarButtonItem即可
self.navigationItem.backBarButtonItem = backItem;
  • 提示:上面这种方式只是修改返回文字而已 至于返回图标还是系统提供的 手势滑动返回依然存在,并且需要在上级界面设置

1、以上三种方法不改变系统手势滑动返回
2、不能实时修改返回按钮的文字

第四种

//创建一个UIButton
UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
//设置UIButton的图像
[backButton setImage:[UIImage imageNamed:@"left_select_img.png"] forState:UIControlStateNormal];
//[_backButton setTitle:backStr forState:UIControlStateNormal];
//给UIButton绑定一个方法,在这个方法中进行popViewControllerAnimated
[backButton addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
//然后通过系统给的自定义BarButtonItem的方法创建BarButtonItem
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
//覆盖返回按键
self.navigationItem.leftBarButtonItem = backItem;
  • 提示:这种方法会失去手势滑动返回的功能

第五种

对 iOS11 导航返回按钮偏移的问题做了处理

@property (strong, nonatomic) UIButton *backButton;

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.frame = CGRectMake(0, 0, 88, 44);
        [_backButton setImage:[UIImage imageNamed:@"App_back"] forState:UIControlStateNormal];//返回箭头图标和系统的不一致
        //[_backButton setTitle:backStr forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
        
        if (!SYSTEM_VERSION_LESS_THAN(@"11")) {
            _backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -5 * APP_WIDTH /375.0,0,0)];
        }
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_backButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }
    return _backButton;
}

- (void)setBackButton {
    NSString *backStr;
    if ([_unReadMessageNum isEqualToString:@"0"]) {
        backStr = @"消息";
    } else {
        backStr = [NSString stringWithFormat:@"消息(%@)",_unReadMessageNum];
    }
    
    [self.backButton setTitle:backStr forState:UIControlStateNormal];
}

- (void)unReadMessageDidUpdate:(NSNotification *)notification
{
    self.unReadMessageNum = notification.object;
    [self setBackButton];
}

- (void)backItemClick {
    [self.navigationController popViewControllerAnimated:YES];
}
  • 提示:在本控制器中修改,手势滑动返回依然存在
  • SYSTEM_VERSION_LESS_THAN

你可能感兴趣的:(iOS返回按钮修改总结)