UINavigationBar without UINavigationController

2019-04-20 22:19发布

问题:

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.

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

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?

回答1:

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:

//Creating the plain Navigation Bar

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


回答3:

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];
}


回答4:

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!



回答5:

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];

}