how i can change the background color of navigationBar

17,497

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.

Share:
17,497
faisal
Author by

faisal

Updated on June 04, 2022

Comments

  • faisal
    faisal almost 2 years

    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.