ios系列——导航条整理

导航条的操作

  • 导航条的背景设置
//设置导航条的背景颜色
nav.navigationBar.barTintColor = MAINCOLOR;

//设置导航条的背景图片
UINavigationBar *navBar = [UINavigationBar appearance];  
[navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault];  
  • 设置文字颜色
//设置系统级的左按钮右按钮颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//设置导航文字颜色
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
  • 设置按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 100, 44)];
    button.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
 self.navigationItem.rightBarButtonItem = rightItem;


self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮    

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];
  • 设置右边多个按钮
UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:button];
[rigArr addObject:rightItem];
[rigArr addObject:rightItem];
self.navigationItem.rightBarButtonItems = rigArr;
  • 设置一个view到nav上 就可以在view上做很多操作了
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonView.backgroundColor = [UIColor greenColor];
UIButton *exampleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
exampleButton2.frame = CGRectMake(70, 2, 30, 40);
[exampleButton2 setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
exampleButton2.backgroundColor = [UIColor redColor];
[buttonView addSubview:exampleButton2];
[exampleButton2 addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.title = @"首页";

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], NSForegroundColorAttributeName, nil]];

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 0, 100, 44)];
    button.backgroundColor = [UIColor blackColor];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = rightItem;

    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    buttonView.backgroundColor = [UIColor greenColor];

    UIButton *exampleButton1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 2, 30, 40)];
    [buttonView addSubview:exampleButton1];
    exampleButton1.backgroundColor = [UIColor blackColor];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];


    UIButton *exampleButton2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    exampleButton2.backgroundColor = [UIColor redColor];
    [exampleButton2 addTarget:self action:@selector(buttonActivity:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = exampleButton2;

如果设置rightBarButtonItem的宽度小于屏幕一半的宽度
如果设置rightBarButtonItem的宽度小于屏幕一半的宽度

如果设置rightBarButtonItem的宽度大于屏幕一半的宽度
如果设置rightBarButtonItem的宽度大于屏幕一半的宽度

  • 导航条按钮
    当设置了左边的按钮 这个页面的手势回退就用不了了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值