14
votes

First of all, thank you for coming here and help solving my problem. Thank you!!!

In iOS11 beta6, sizeThatFits: seems to not work on UINavigationBar. I notice that UINavigationBar structure has changed by Reveal my app.

I have tried my best to change custom navigation bar's height. But it seems always to be 44, and it works before iOS11.

- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width, 64);
    return newSize;
}

enter image description here

Oddly, I just log its frame in didMoveToSuperview method, its height is 64, but I really see that is 44 in Reveal and app.

I have no idea about this... Help me please.. Thank you.

Update:

I found that about my custom navigation bar LayoutConstraints log in console like this :

"<NSAutoresizingMaskLayoutConstraint:0x604000495ae0 FDCustomNavigationBar:0x7fe2f01399d0.(null) == 42>",
"<NSAutoresizingMaskLayoutConstraint:0x604000495b30 FDCustomNavigationBar:0x7fe2f01399d0.height == 44>"`

bug I even no use auto layout in my navigation bar. What's wrong with it?

Update 8/28 :

enter image description here

I have set my custom navigation bar's subviews frame at navigation bar's - layoutSubviewsmethod.

- (void)layoutSubviews {
    [super layoutSubviews];

    self.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 64);

    for (UIView *view in self.subviews) {

        if([NSStringFromClass([view class]) containsString:@"Background"]) {
            view.frame = self.bounds;
        } else if ([NSStringFromClass([view class]) containsString:@"ContentView"]) {
            CGRect frame = view.frame;
            frame.origin.y = 20;
            frame.size.height = self.bounds.size.height - frame.origin.y;
            view.frame = frame;
        }
    }
}

but the navigation bar will cover view controller's view. How can I fix that?

2
@SaurabhJain Thanks. I have seen it before I ask this question. That doesn't work for me.Jerry4me
@Jerry4me were you able to find a way to fix it? I'm facing the same issue and nothing works for me as well.nominanza

2 Answers

11
votes

Since iOS 11 UINavigationBar fully supports Auto Layout (this is the reason why you're seeing its constraints). I've opened a radar to Apple because I thought that setting a height constraint to the titleView would have adjusted the navigation bar height accordingly. However this is what Apple replied:

Full support for auto layout does not imply that your view can influence other aspects of the layout of the navigation bar – in particular, the navigation bar enforces its own height and does not allow the title view or other custom views to exceed the height of the navigation bar. We are continuing to work on this issue, and will follow up with you again.

As of today the radar is still open.

0
votes

Hello I just experienced this same issue.

Now the top layout guide is deprecated on iOS 11. You have to reference the safeAreaLayoutGuide in your constraints.

Here's an example in swift

    if #available(iOS 11, *) {
        let guide = self.view.safeAreaLayoutGuide.topAnchor
        let height = (self.navigationController?.navigationBar.frame.height)! - CGFloat(12)
        NSLayoutConstraint.activate([
        self.yourTableView.topAnchor.constraint(equalTo: guide, constant: height)
        ])
    }

As you can see your view's top anchor should match the safeAreaLayoutGuide top anchor. In this example I'm using the variable height to create the new constraint. The variable height contains the navigation bar height minus a constant.

You should try by changing the height value.

Hope this helps you.