自定义导航按钮UIBarButtonItem
基本上每个iOS APP里面都有导航,比如微信、QQ、支付宝。导航可以很方便地帮助我们管理视图控制器(UIViewController)。导航的重要性不言而喻,基本上是每一位iOS初学者都要接触到的问题。
iOS系统导航栏中有leftBarButtonItem
和rightBarButtonItem
,我们可以根据自己的需求来自定义这两个UIBarButtonItem
。
系统提供了几种的创建方法
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (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;
- (instancetype)initWithCustomView:(UIView *)customView;
通过系统的创建UIBarButtonItem
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];
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];
}