How can I change the background color of the navigationBar

In my navigation bar, I have two buttons in the left and right positions and a segment view in the navigation header. I want to change the background color of the navigation bar to black, but the color of the elements on it will be a different color. How can I do it? I am trying to change the color of the tintColor navigation bar to black. but I will show that the color of the segmented control button and the button on the navigation bar also changed to black.

early.

+3
source share
3 answers

try setting objects as subzones in the navigationBar.

Set hue color

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, subviews. UIBarButtonsItems , self.navigationItem.rightBarButtonItem = tempButton; , .

, , . , .

+12

tintColor . .

0
 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

Source: https://habr.com/ru/post/1721862/


All Articles