White space under the navigation bar

2019-08-05 08:56发布

When self.navigationController.navigationBar.translucent = NO, a white space occur under the navigation bar, like this:

Sample screenshot

How can I remove it?

// From above example
(void)loadUI
{
    [self.countriesButton setBackgroundColor:[UIColor colorWithRed:(243/255.0) green:(178/255.0) blue:(128/255.0) alpha:1]];
    [self.countriesButton.layer setCornerRadius:10];
    [self.countriesButton setClipsToBounds:YES];

    /// set attributes for Navigation Bar
    self.navigationItem.title = @"Welcome"; // Title of Navigation Bar
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:(243/255.0) green:(178/255.0) blue:(128/255.0) alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    self.navigationController.navigationBar.translucent = NO;
}

5条回答
别忘想泡老子
2楼-- · 2019-08-05 09:05

Im gonna chime in how i solved this one since it was easy and worked well.

In your storyboard select the view controller that is getting the whitespace under the opaque navbar.

Then go to 'Attributes Inspector' -> 'View Controller' -> 'Extend Edges' -> Check the box that says 'Under Opaque Bars'

查看更多
可以哭但决不认输i
3楼-- · 2019-08-05 09:05

The problem happens when you switch between translucent and opaque. I don't really know why, but the view is automatically resized. It is the same when you switch from opaque to translucent, the view expands at the top.

My guess is that you are probably setting somewhere else the bar as translucent. In the xib file or previously in the code.

查看更多
孤傲高冷的网名
4楼-- · 2019-08-05 09:16

Try with

self.automaticallyAdjustsScrollViewInsets = NO

This is introduced in iOS7 and app will crash if you don't check system version before calling. You can also set this from storyboard 'Adjust Scroll View Insets'

enter image description here

查看更多
来,给爷笑一个
5楼-- · 2019-08-05 09:21

Try

self.wantsFullScreenLayout = YES;
查看更多
SAY GOODBYE
6楼-- · 2019-08-05 09:21

try adding a 0 px image as shadow of navbar

[self.navigationController.navigationBar setShadowImage:[UIImage new]];
查看更多
登录 后发表回答