在写项目的工程中,我们可能会遇到各种各样的项目,写的方法也是各有不同,不喜欢自定义的小伙伴也很多,
下面我就记录下系统导航和barbuttonitem的修改系统空间的方法:
1,添加rightbarbuttonitem的方法
UIImage* img=[UIImage imageNamed:@"logoff.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame =CGRectMake(0, 0, 32, 32);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn addTarget: self action: @selector(exitAction) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem* item=[[UIBarButtonItemalloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem=item;
2,BarButtonItem设置背景图片,颜色更改解决办法
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"MoreImage"] style:UIBarButtonItemStylePlain target:self action:@selector(moreItemPress)];
用这种方法,设置的背景图片,颜色变掉了。不是原来设置的图片颜色。
可以用另外一种方式,可以解决变色的问题:
UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
[rightButton setImage:[UIImage imageNamed:@"MoreImage"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(moreItemPress) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem= rightItem;
3,修改title字体颜色,导航栏背景颜色,返回字体等
//改变系统导航title的字体颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//改变系统导航title的字体大小和自定义字体
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:MainFont(19),NSForegroundColorAttributeName:[UIColor whiteColor]}];
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;