UINavigationController常用属性设置

来源:互联网 发布:stm32 3轴控制算法 编辑:程序博客网 时间:2024/05/29 15:20
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 1 设置导航栏标题  
  2. // 方法1  
  3. self.title = @"导航栏控制器";  
  4. // 方法2 当有tabbarController时使用该方法,避免影响标题设置  
  5. self.navigationItem.title = @"导航栏控制器标题";  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 2 设置自定义导航栏标题子视图  
  2. UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.00.040.040.0)];  
  3. titleImageView.image = [UIImage imageNamed:@"image"];  
  4. self.navigationItem.titleView = titleImageView;  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 3 设置导航栏控制器-右按钮  
  2. // 3-1 设置单个按钮  
  3. // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleDone target:self action:@selector(nextClick:)];  
  4. // 3-2 设置多个按钮  
  5. UIBarButtonItem *playItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(nextClick:)];  
  6. UIBarButtonItem *stopItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(doneClick:)];  
  7. NSArray *itemsRight = [NSArray arrayWithObjects:playItem, stopItem, nil nil];  
  8. self.navigationItem.rightBarButtonItems = itemsRight;  
  9.   
  10. - (void)nextClick:(UIBarButtonItem *)barButton  
  11. {  
  12.     NSLog(@"你点击了 next ");  
  13. }  
  14.   
  15. - (void)doneClick:(UIBarButtonItem *)barButton  
  16. {  
  17.     NSLog(@"你点击了 done ");  
  18. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 4 设置导航栏控制器-左按钮  
  2. // 4-1 设置单个按钮  
  3. // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(sureClick:)];  
  4. // 3-2 设置多个按钮  
  5. UIBarButtonItem *sureItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sureClick:)];  
  6. UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelClick:)];  
  7. NSArray *itemsLeft = [NSArray arrayWithObjects:sureItem, cancelItem, nil nil];  
  8. self.navigationItem.leftBarButtonItems = itemsLeft;  
  9.   
  10. - (void)sureClick:(UIBarButtonItem *)barButton  
  11. {  
  12.     NSLog(@"你点击了 sure ");  
  13. }  
  14.   
  15. - (void)cancelClick:(UIBarButtonItem *)barButton  
  16. {  
  17.     NSLog(@"你点击了 cancel ");  
  18. }  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 5 设置导航栏控制器显示样式  
  2. // 样式  
  3. self.navigationController.navigationBar.barStyle = UIBarStyleDefault;  
  4. // 导航按钮颜色  
  5. self.navigationController.navigationBar.tintColor = [UIColor greenColor];  
  6. // 背景颜色  
  7. self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];  
  8. // 背景图片  
  9. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image"] forBarMetrics:UIBarMetricsDefault];  
  10. // 背景阴影  
  11. self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"image"];  
  12. // 字体标题设置(字体大小、字体颜色设置)  
  13. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0],  NSForegroundColorAttributeName:[UIColor yellowColor]}];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 6 导航栏控制器的显示,或隐藏  
  2. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10.080.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 30.0)];  
  3. [self.view addSubview:button];  
  4. button.backgroundColor = [UIColor redColor];  
  5. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  6. [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];  
  7. [button setTitle:@"隐藏导航栏" forState:UIControlStateNormal];  
  8. [button setTitle:@"显示导航栏" forState:UIControlStateSelected];  
  9. [button addTarget:self action:@selector(hiddenClick:) forControlEvents:UIControlEventTouchUpInside];  
  10.   
  11. - (void)hiddenClick:(UIButton *)button  
  12. {  
  13.     button.selected = !button.selected;  
  14.       
  15.     if (button.selected)  
  16.     {  
  17. //        self.navigationController.navigationBarHidden = YES; // 方法1  
  18.         [self.navigationController setNavigationBarHidden:YES animated:YES]; // 方法2 常用方法,特别是视图控制器之间跳转时避免产生黑色区域  
  19.     }  
  20.     else  
  21.     {  
  22. //        self.navigationController.navigationBarHidden = NO;  
  23.         [self.navigationController setNavigationBarHidden:NO animated:YES];  
  24.     }  
  25. }  




0 0