美文网首页
自定义导航按钮

自定义导航按钮

作者: Leon1024 | 来源:发表于2018-11-19 00:58 被阅读0次

最简单去掉back字样,保留原图标,修改图标颜色为黑色

    self.navigationController.navigationBar.topItem.title = @"";
    [self.navigationController.navigationBar setTintColor:UIColor.blackColor];

只带文字的导航按钮

注意,自定义按钮后,手势滑动返回将失效。如果需要恢复手势滑动返回,需要遵循 <UIGestureRecognizerDelegate> 协议

// 设置自定义的导航按钮
- (void)customNavigationItem{
     UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(gotoViewController)];
    // 字体颜色
    [item setTintColor:UIColor.blackColor];
    self.navigationItem.leftBarButtonItem = item;
    // 恢复手势滑动返回
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

// 跳转视图
- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

只带图片的导航按钮

// 设置自定义的导航按钮
- (void)customNavigationItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rule_navbar_icon_ruturn"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoViewController)];
    // 设置图片的底色
    [item setTintColor:UIColor.blackColor];
    self.navigationItem.leftBarButtonItem = item;
    // 恢复手势滑动返回
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

// 返回上级视图
- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

带图文的导航按钮

// 自定义图文返回按钮
- (void)customNavigationItem {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:@"rule_navbar_icon_ruturn"] forState:UIControlStateNormal];
    [button setTitle:@" 返回" forState:UIControlStateNormal];
    [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [button addTarget:self action:@selector(gotoViewController) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = item;
    // 保留手势,遵循 <UIGestureRecognizerDelegate>
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)gotoViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

相关文章

网友评论

      本文标题:自定义导航按钮

      本文链接:https://www.haomeiwen.com/subject/obgnfqtx.html