iOS开发之--改变系统导航的颜色,字体,还有返回样式的自定义
在写项目的工程中,我们可能会遇到各种各样的项目,写的方法也是各有不同,不喜欢自定义的小伙伴也很多,
下面我就记录下系统导航和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;
//为barbuttonitem的返回字体添加字体颜色
self.navigationController.navigationBar.tintColor = MainColor;
//为barbuttonitem添加自定义图片
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage imageNamed:@"backBarButton"] forState:(UIControlStateNormal) barMetrics:(UIBarMetricsDefault)];
本文来自博客园,作者:稻草人11223,转载请注明原文链接:https://www.cnblogs.com/hero11223/p/5981214.html