美文网首页ios
导航栏那些事

导航栏那些事

作者: iOS打怪升级 | 来源:发表于2017-05-18 16:01 被阅读226次

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

    通过设置当前页面的leftBarButtonItem 来替换系统自带的back 按钮,下面是系统UINavigationBar的头文件部分关于leftBarButtonItem 的阐述
    @interface UINavigationBar : UIView <NSCoding, UIBarPositioning> 
    /* 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
    自定义left 组合.gif

    2.待续。。。

    相关文章

      网友评论

        本文标题:导航栏那些事

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