简体   繁体   English

我怎么能改变navigationBar的背景颜色

[英]how i can change the background color of navigationBar

In my navigation bar i have two button at left and right position and a segmentetControll view at navigation title. 在我的导航栏中,我在左侧和右侧位置有两个按钮,在导航标题处有一个segmentetControll视图。 I want to change the background color of the navigation bar to black but thae color of the items on it will be another color. 我想将导航栏的背景颜色更改为黑色,但其上的项目颜色将是另一种颜色。 how can i do it? 我该怎么做? I try to chnage the tintColor of the navigationBar to black. 我尝试将navigationBar的tintColor设置为黑色。 but i show that the color of the segmentedControll and the button on the navigationBar also changed to black. 但我表明segmentedControll的颜色和navigationBar上的按钮也变为黑色。

thanks in advance. 提前致谢。

try to set the objects as subviews to the navigationBar. 尝试将对象设置为navigationBar的子视图。

Set the tint color 设置色调颜色

UINavigationController *theNavigationController = [[UINavigationController alloc] initWithRootViewController: aFeedControler]; 
theNavigationController.navigationBar.tintColor = [UIColor blackColor];

Add the segmentedControl as a subview in the viewControllers like this: 在viewControllers中添加segmentedControl作为子视图,如下所示:

 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;

For the buttons you should try creating UIButtons not UIBarButtonsItems and add them as subviews also. 对于按钮,您应该尝试创建UIButtons而不是UIBarButtonsItems,并将它们添加为子视图。 If you create UIBarButtonsItems and add them like this self.navigationItem.rightBarButtonItem = tempButton; 如果您创建UIBarButtonsItems并像这样添加它们self.navigationItem.rightBarButtonItem = tempButton; you will get the effect that you saw. 你会得到你看到的效果。

if you add them as subviews you shouldn't have the problem you mentioned.. hope it helps. 如果你把它们作为子视图添加,你不应该有你提到的问题..希望它有所帮助。

 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

You have the property called tintColor in navigation bar. 您在导航栏中有名为tintColor的属性。 You can set its value to any colour. 您可以将其值设置为任何颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM