自定义导航按钮UIBarButtonItem

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

iOS系统导航栏中有leftBarButtonItemrightBarButtonItem,我们可以根据自己的需求来自定义这两个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];

}

 

转载于:https://www.cnblogs.com/xiqiyangyang/p/4698618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值