基本上每一个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];
}