iOS開發>學無止境 - 自定義導航按鈕UIBarButtonItem


基本上每個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,那么想怎么定義都是可以的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM