UINavicationController基本应用

本文详细介绍了UINavigationController的使用,包括设置导航栏的透明度、标题、颜色、左右按钮及字体颜色,以及添加UISegmentedControl作为titleView并实现点击事件。通过示例代码,演示了导航栏自定义的各种方法,并预告了如何与UITabBarController结合使用的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UINavicationController 导航栏,顾名思义为视图与视图之间切换的桥梁,简单来说,就是用来控制视图之间的跳转


请看以下代码分析:

  1. // 设置navigationbar的半透明效果
  2.     self.navigationController.navigationBar.translucent = NO;
  3.     [self.navigationController.navigationBar setTranslucent:NO];
  4. // 设置navigationbar上显示的标题 
  5.     self.title = @"navigationcontroller"; 
  6. // 设置navigationbar的颜色
  7.     [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];  
  8. // 设置navigationbar左边按钮 
  9.     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil]; 
  10. // 设置navigationbar右边按钮  
  11.     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];
  12. // 也可以自定义navigationbar 左右的按钮
  13. 思路:新建按钮控件rightbtn,然后 initWith  rightbtn 得到UIBarButtonItem,最后赋值给rightBarButtonItem

  1. // 设置navigationbar上左右按钮字体颜色
  2.     [self.navigationController.navigationBar setTintColor:[UIColor blueColor]];


设置导航条里面分段效果

  1. // 设置navigation上的titleview 
  2.     NSArray *arr = [NSArray arrayWithObjects:@"左边",@"右边", nil nil];  
  3.     UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arr];  
  4.     segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
  5. // 添加点击事件
  6.    [segmentedController addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
  7.     self.navigationItem.titleView = segment; 

UISegmentedControl  分段控制,这个分段可以分为两个视图来控制,添加点击事件,根据index判断哪个分段被点击,执行相应的操作。


  1. -(IBAction)segmentClick:(id)sender  
  2. {  
  3.     switch ([sender selectedSegmentIndex]) {  
  4.         case 0:  
  5.         {  
  6.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了左边" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  7.             [alter show];  
  8.    // 执行相应的操作
  9.         }  
  10.         break;  
  11.     case 1:  
  12.         {  
  13.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了右边" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  14.             [alter show];  
  15. // 执行相应的操作
  16.         }  
  17.         break;  
  18.           
  19.         default:  
  20.             break;  
  21.     }  
  22. }  


如何结合UITabbarController 一起使用,敬请期待



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值