导航栏那些事

1.自定义返回按钮的问题

通过设置当前页面的leftBarButtonItem 来替换系统自带的back 按钮,下面是系统UINavigationBar的头文件部分关于leftBarButtonItem 的阐述
@interface UINavigationBar : UIView  
/* By default, the leftItemsSupplementBackButton property is NO. In this case,
 the back button is not drawn and the left item or items replace it. If you
 would like the left items to appear in addition to the back button (as opposed to instead of it)
 set leftItemsSupplementBackButton to YES.
 */
@property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
// Some navigation items want to display a custom left or right item when they're on top of the stack.
// A custom left item replaces the regular back button unless you set leftItemsSupplementBackButton to YES
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@end

意思:一个自定义的left item 将会替换系统的返回按钮,除非你设置leftItemsSupplementBackButton 属性为Yes ,back 按钮才不会被替换

下面是实现代码:

    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"自定义" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)]];

     或者设置left items 组合按钮,比如在webView 的控制器中使用是需要两个按钮
    UIBarButtonItem * test1 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)];
    UIBarButtonItem * test2 = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)];
    [self.navigationItem setLeftBarButtonItems:@[test1,test2]];

如果想让自己设置的left item 失效,采用系统的back ,采用下面的代码:

 [self.navigationItem setLeftBarButtonItem:nil];

效果如下


自定义返回按钮.gif

导航栏那些事_第1张图片
自定义left 组合.gif

2.待续。。。

你可能感兴趣的:(导航栏那些事)