基本上每一個iOS APP裏面都有導航,好比微信、QQ、支付寶。導航能夠很方便地幫助咱們管理視圖控制器(UIViewController)。導航的重要性不言而喻,基本上是每一位iOS初學者都要接觸到的問題。微信
iOS系統導航欄中有leftBarButtonItem
和rightBarButtonItem
,咱們能夠根據本身的需求來自定義這兩個UIBarButtonItem
。ide
系統提供了幾種的建立方法code
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;orm
- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action NS_AVAILABLE_IOS(5_0); // landscapeImagePhone will be used for the bar button image when the bar has Compact or Condensed bar metrics.圖片
- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;支付寶
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;get
- (instancetype)initWithCustomView:(UIView *)customView;it
經過系統的建立UIBarButtonItemio
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];ast
BarButtonSystemItem 點進去看是一個枚舉 在這裏也不過多的介紹了
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,
別忘了實現right:方法了
- (void)right:(id)sender {
NSLog(@"點擊了導航欄右邊的按鈕");
}
自定義文字的UIBarButtonItem
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(right1)];
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,
};
自定義圖片
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"圖片名"] style:UIBarButtonItemStylePlain target:self action:@selector(right)];
自定義view
UIView *BarbuttonItemView = [[UIView alloc] init];
BarbuttonItemView.frame = CGRectMake(0, 0, 20, 20);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:BarbuttonItemView];
能夠徹底的自定義 寫一個分類
/**
設置UIBarButtonItem的按鈕
*/
+ (UIBarButtonItem *)itemWithimageNdamed: (NSString *)imageNamed HighlightedimageNamed:(NSString *)HighlightedimageNamed target:(id)target action:(SEL)action;
+ (UIBarButtonItem *)itemWithimageNdamed: (NSString *)imageNamed HighlightedimageNamed:(NSString *)HighlightedimageNamed target:(id)target action:(SEL)action {
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:HighlightedimageNamed] forState:UIControlStateHighlighted];
button.size = button.currentBackgroundImage.size;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}