繁体   English   中英

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

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

在我的导航栏中,我在左侧和右侧位置有两个按钮,在导航标题处有一个segmentetControll视图。 我想将导航栏的背景颜色更改为黑色,但其上的项目颜色将是另一种颜色。 我该怎么做? 我尝试将navigationBar的tintColor设置为黑色。 但我表明segmentedControll的颜色和navigationBar上的按钮也变为黑色。

提前致谢。

尝试将对象设置为navigationBar的子视图。

设置色调颜色

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

在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;

对于按钮,您应该尝试创建UIButtons而不是UIBarButtonsItems,并将它们添加为子视图。 如果您创建UIBarButtonsItems并像这样添加它们self.navigationItem.rightBarButtonItem = tempButton; 你会得到你看到的效果。

如果你把它们作为子视图添加,你不应该有你提到的问题..希望它有所帮助。

 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

您在导航栏中有名为tintColor的属性。 您可以将其值设置为任何颜色。

暂无
暂无

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

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