开发者

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. 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. but i show that the color of the segmentedControll and the button on the navigationBar also changed to black.

thanks in advance.


try to set the objects as subviews to the 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:

 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. If you create UIBarButtonsItems and add them like this 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.


You have the property called tintColor in navigation bar. You can set its value to any colour.


 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜