iOS 导航栏控制器UINavigationController 、UINavigationBar、UINavigationItem 之间的关系(二)

UINavigationBar相关属性

UINavigationItem相关属性

1、导航栏标题内容

@property(nullable, nonatomic,copy)   NSString        *title; 

2、自定义导航栏上显示的内容,可以添加按钮等其他显示内容。某些app中在导航栏上左边显示内容,旁边有个下拉按钮等

@property(nullable, nonatomic,strong) UIView          *titleView;

示列:注意高度不能超过导航栏的高度

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
 customView.backgroundColor = [UIColor redColor];
  self.navigationItem.titleView = customView;

3、设置prompt属性的话,整个导航栏上部分会多出一部分高度30px,来显示对应的描述文字

@property(nullable,nonatomic,copy)   NSString *prompt

这里写图片描述

4、隐藏返回按钮,注意需要在viewWillAppear方法中写,e.g:A push 到B,则需要在B的viewWillAppear中;

//都可以隐藏返回按钮
@property(nonatomic,assign) BOOL hidesBackButton
- (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;

示列:

//进入该页面时隐藏,离开时显示
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappea:animatedr];
   self.navigationItem.hidesBackButton = NO;
}

5、自定义返回按钮的样式,可以是文字或者是图片,注意这个属性,若是A push B,你若是在B中设置backBarButtonItem属性,那么起作用的是Bpush到的下个页面,要是B中的返回按钮样式也修改,可以在A中设置这个属性,或者在B中使用leftBarButtonItem属性,大部分我们都是用leftBarButtonItem属性

@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem

示列:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];

6、可设置当前页面的返回按钮和右上角的按钮

@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;

@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;

- (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
- 
- (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;

示列:

//返回按钮
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];

//右上角按钮
 rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"edit"] style:UIBarButtonItemStylePlain target:self action:@selector(edit)];

7、有时候左上角和右上角有多个按钮,那么可以用如下的方法

@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems ;

@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems ;

- (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;
- 
- (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated ;

示列:

 //右边第一个
        UIButton* rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
//        [rightBt setBackgroundColor:[UIColor redColor]];
        rightBt.frame = CGRectMake(0, 0, 30, 30);

        [rightBt addTarget:self action:@selector(addEvent) forControlEvents:UIControlEventTouchUpInside];
        [rightBt setImage:[UIImage imageNamed:@"addBtn.png"] forState:UIControlStateNormal];
//        [rightBt setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20)]; // 向右边拉伸

        UIButton* rightBt2 = [UIButton buttonWithType:UIButtonTypeCustom];
//        [rightBt2 setBackgroundColor:[UIColor yellowColor]];
        rightBt2.frame = CGRectMake(0, 0, 20, 19);
        [rightBt2 addTarget:self action:@selector(toYear) forControlEvents:UIControlEventTouchUpInside];
        [rightBt2 setImage:[UIImage imageNamed:@"calendarIcon.png"] forState:UIControlStateNormal];
        rightBt2.imageView.contentMode = UIViewContentModeScaleAspectFit;
//        [rightBt2 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -35)]; // 向右边拉伸

        UIBarButtonItem* rightItem1 = [[UIBarButtonItem alloc] initWithCustomView:rightBt];
        UIBarButtonItem* rightItem2 = [[UIBarButtonItem alloc] initWithCustomView:rightBt2];

        self.navigationItem.rightBarButtonItems = @[rightItem1, rightItem2];

7、back按钮即有图片,又有“返回”的文字

UIButton *btnb = [UIButton buttonWithType : UIButtonTypeCustom];
    btnb.frame = CGRectMake (0, 0, 44, 44);
    btnb.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [btnb setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    btnb.titleLabel.font=[UIFont boldSystemFontOfSize:14.f];
    [btnb setTitle:showName forState:UIControlStateNormal];
    [btnb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnb.showsTouchWhenHighlighted=YES;
    [btnb addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];

    //用到了UIBarButtonItem的initWithCustomView初始化方式
    UIBarButtonItem *ubar=[[UIBarButtonItem alloc] initWithCustomView :btnb];
    self.navigationItem.leftBarButtonItem = ubar;

更多详细说明

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值