11
votes

I want to have a UINavigationBar in the top of my app, just like in the phone app for example, but I don't want to use it with a UINavigationController, that is, I want to use it as a "standalone" object.

The view controller that will have the UINavigationBar has its view in a .xib file. I tried to drag an instance of UINavigationBar from the Object Library and it works fine, but the status bar of the app is still white while the UINavigationBar is gray. I want the status bar to have the same gray tone, but not the rest of the view.

To show you what I mean, I have two pictures. The first is the phone app. Notice that the status bar and the navigation bar is gray but the background is white.

iPhone Phone App

The following picture is from my app, as you can see the status bar is white (I want it to be gray as well).

My iPhone app

I have also tried to use the following code in the view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Köp ProViva";
}

I tried it both with and without the UINavigationBar but either no navigation bar shows up or it looks the same as before.

How can I add a UINavigationBar and have the status bar have the same color?

6
"the status bar of the app is still white while the UINavigationBar is gray". Please explain how the UINavigationBar is gray. It looks white to me (with black text)staticVoidMan
@staticVoidMan I think it looks like gray, but it is a very light-gray :Duser3124010
hm... super light indeed. so... you want the background color of statusBar and navigationBar as lightGray?staticVoidMan
@staticVoidMan Yes, exactly. The background color already is light gray by default, but the status bar is white. I think that the status bar will change to light gray if you manage to set the UINavigationBar in a special way (like the answer by Rushabh).user3124010
ok, I posted an answer. hopefully that might helpstaticVoidMan

6 Answers

16
votes

You can implement the delegate method -positionForBar: of the UINavigationBarDelegate protocol and simply return UIBarPositionTopAttached.

Example:

-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

//if you're not using a `UINavigationController` and instead
//simply want a `UINavigationBar` then use the following method as well
-(void)testMethod
{
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setDelegate:self];
    [self.view addSubview:navBar];
}

Hope this helps.

2
votes

//Creating the plain Navigation Bar

UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.topItem.title = @"title";
[self.view addSubview:headerView];
2
votes

You can't modify the status bar background color directly but since it is transparent (since iOS7), you can place a UIView of 20px in height behind the status bar with the desired color and it will appear as if the status bar has a background color.

As for the UINavigationBar, it's just modifying a UINavigationBar object that will help.

Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //...

    //ISSUE 1: StatusBar Background
    UIView *vwStatusBarUnderlay = [[UIView alloc] init];
    [vwStatusBarUnderlay setFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    [vwStatusBarUnderlay setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:vwStatusBarUnderlay];
    //[vwStatusBarUnderlay sendSubviewToBack:self.view];

    //ISSUE 2: NavigationBar
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setTranslucent:NO];

    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    [navItem setTitle:@"Favoriter"];
    [navItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]];

    [navBar setItems:@[navItem]];
    [self.view addSubview:navBar];
}
1
votes

First answer worked well for me, but here it's in Swift instead of Objective-C:

1- you set the delegate of the navbar to the viewcontroller, whether via storyboard or the code.
2- add UINavigationBarDelegate to your controller protocols or extend it as follow:

extension ViewController: UINavigationBarDelegate {
    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
}

That's it, works perfectly!

0
votes

We can't set the color of the status bar by our choice, what we can do is;

In the AppDelegate.m file;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      //Set status bar style here
       [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

}
0
votes
class TopAttachedNavigationBarDelagete: NSObject, UINavigationBarDelegate {
    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return .topAttached
    }
}
  1. In Interface Builder add an Object and set it's class to TopAttachedNavigationBarDelagete
  2. Select an UINavigationBar and in Connections Inspector connect the delegate property with previously created object