• iOS开发>学无止境


    基本上每个iOS APP里面都有导航,比如微信、QQ、支付宝。导航可以很方便地帮助我们管理视图控制器(UIViewController)。导航的重要性不言而喻,基本上是每一位iOS初学者都要接触到的问题。

    iOS系统导航栏中有leftBarButtonItemrightBarButtonItem,我们可以根据自己的需求来自定义这两个UIBarButtonItem

    四种创建方法

    系统提供了四种创建的方法:

    - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
    
    - (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
    
    
    - (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
    
    - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
    
    - (instancetype)initWithCustomView:(UIView *)customView;

    通过系统UIBarButtonSystemItem创建

    自定义rightBarButtonItem,代码如下:

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];

    UIBarButtonSystemItem有以下样式可以供选择:

    typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
        UIBarButtonSystemItemDone,
        UIBarButtonSystemItemCancel,
        UIBarButtonSystemItemEdit,  
        UIBarButtonSystemItemSave,  
        UIBarButtonSystemItemAdd,
        UIBarButtonSystemItemFlexibleSpace,
        UIBarButtonSystemItemFixedSpace,
        UIBarButtonSystemItemCompose,
        UIBarButtonSystemItemReply,
        UIBarButtonSystemItemAction,
        UIBarButtonSystemItemOrganize,
        UIBarButtonSystemItemBookmarks,
        UIBarButtonSystemItemSearch,
        UIBarButtonSystemItemRefresh,
        UIBarButtonSystemItemStop,
        UIBarButtonSystemItemCamera,
        UIBarButtonSystemItemTrash,
        UIBarButtonSystemItemPlay,
        UIBarButtonSystemItemPause,
        UIBarButtonSystemItemRewind,
        UIBarButtonSystemItemFastForward,
    #if __IPHONE_3_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
        UIBarButtonSystemItemUndo,
        UIBarButtonSystemItemRedo,
    #endif
    #if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
        UIBarButtonSystemItemPageCurl,
    #endif
    };

    最后别忘了实现right:方法:

    - (void)right:(id)sender
    {
        NSLog(@"rightBarButtonItem");
    }

    自定义文字的UIBarButtonItem

    在文章关于导航栏的六个小技巧的第五个技巧里面有自定义rightBarButtonItem

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];

    UIBarButtonItemStyle有以下三种选择:

    typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
        UIBarButtonItemStylePlain,
        UIBarButtonItemStyleBordered NS_ENUM_DEPRECATED_IOS(2_0, 8_0, "Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later"),
        UIBarButtonItemStyleDone,
    };

    实现back:方法:

    - (void)back:(id)sender
    {
        [self.navigationController popViewControllerAnimated:YES];
    }

    自定义照片的UIBarButtonItem

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"test"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];

    自定义UIView的UIBarButtonItem

    自定义UIView,然后通过initWithCustomView:方法来创建UIBarButtonItem

     UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];

    看到有朋友在后台提问:

    刚哥,我现在即需要改那个导航原生的返回图片,也要改返回文字,应该怎么改呢,求指教。

    其实,这个就可以用initWithCustomView:来解决,自定义UIView你可以放UIImageViewUILabel。可以自定义UIView,那么想怎么定义都是可以的。

    本内容来自: 超越昨天(学无止境) - http://www.cnblogs.com/xvewuzhijing/
  • 相关阅读:
    数据结构与算法JavaScript (一) 栈
    js架构设计模式——前端MVVM框架设计及实现(二)
    js架构设计模式——前端MVVM框架设计及实现(一)
    js架构设计模式——MVC,MVP 和 MVVM 的图示及简单明了的区别说明
    js架构设计模式——你对MVC、MVP、MVVM 三种组合模式分别有什么样的理解?
    js面向对象oop编程
    js模块化开发——前端模块化
    SPRING 集成 activemq 的 topic 模式
    linux yum 本地源配置
    ORACLE 导入的问题
  • 原文地址:https://www.cnblogs.com/xvewuzhijing/p/5003823.html
Copyright © 2020-2023  润新知