简体   繁体   中英

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.

 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. You can set its value to any colour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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