【问题标题】:how i can change the background color of navigationBar我如何更改导航栏的背景颜色
【发布时间】:2009-11-04 09:20:36
【问题描述】:
在我的导航栏中,我在左右位置有两个按钮,在导航标题处有一个 segmentetControll 视图。
我想将导航栏的背景颜色更改为黑色,但其上项目的颜色将是另一种颜色。
我该怎么做?
我尝试将导航栏的 tintColor 更改为黑色。
但是我显示segmentedControll的颜色和navigationBar上的按钮也变成了黑色。
提前致谢。
【问题讨论】:
标签:
iphone
uinavigationcontroller
navigation
uinavigationbar
uinavigationitem
【解决方案1】:
尝试将对象设置为导航栏的子视图。
设置色调
UINavigationController *theNavigationController = [[UINavigationController alloc] initWithRootViewController: aFeedControler];
theNavigationController.navigationBar.tintColor = [UIColor blackColor];
将 segmentedControl 添加为 viewControllers 中的子视图,如下所示:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:[UIImage imageNamed:@"segment_check.png"],
[UIImage imageNamed:@"segment_search.png"],
[UIImage imageNamed:@"segment_tools.png"], nil]];
CGRect frame = CGRectMake(0,0, 200,40);
segmentedControl.frame = frame;
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
self.navigationItem.titleView = segmentedControl;
对于按钮,您应该尝试创建 UIButtons 而不是 UIBarButtonsItems,并将它们也添加为子视图。如果您创建 UIBarButtonsItems 并像这样添加它们 self.navigationItem.rightBarButtonItem = tempButton;你会得到你看到的效果。
如果您将它们添加为子视图,您应该不会遇到您提到的问题..希望它有所帮助。
【解决方案2】:
您在导航栏中拥有名为 tintColor 的属性。您可以将其值设置为任何颜色。
【解决方案3】:
UINavigationController *_navigationController = [[UINavigationController alloc] initWithRootViewController: _viewController];
either this
_navigationController.navigationBar.tintColor = [UIColor blackColor];
OR
_navigationController.navigationBar.barStyle=UIBarStyleBlack;
Now about right and left buttons:
UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame=CGRectMake(0.0, 5.0, 50.0, 30.0);
btnLeft.backgroundColor=[UIColor RedColor];
btnLeft.layer.borderColor=[UIColor whiteColor].CGColor;
btnLeft.titleLabel.textColor=[UIColor blackColor];
btnLeft.titleLabel.font=[UIFont boldSystemFontOfSize:10.0];
_navigationController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:btnLeft];
similary you can add right bar button