9
votes

The navigation bar width stays 320 even for iPhone 6 (which needs 375)

There is no change of navigation bar appearance or frame via code.

Simple view controller embedded in UINavigationController

Simulator-Screenshot

Xcode says the width on the storyboard is still 320, (and it doesn't allow me to change).

Storyboard-Screenshot

EDIT: The Navigation Controller in question has a storyboard identifier and is being presented modally over another view controller.

3
where does this storyboard navbar default value (320) comes from ?? it must be in some initial build settings or plist.Nilesh Ukey
Is this a new Xcode 6 project or is it just an older project opened in Xcode 6?cojoj
It is a new Xcode 6 project, but i originally had forgotten to add launch-images... The nav-bar was working then... After i added the images, the nav bar started acting weird.sambhav7890

3 Answers

2
votes

try adding your navigation controller programatically and then push or present the view controller. This should work

0
votes

I had the same problem implementing this category:

//  UINavigationBar+CustomHeight.m
//
//  Copyright (c) 2014 Maciej Swic
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

#import "UINavigationBar+CustomHeight.h"
#import "objc/runtime.h"

static char const *const heightKey = "Height";

@implementation UINavigationBar (CustomHeight)

- (void)setHeight:(CGFloat)height
{
    objc_setAssociatedObject(self, heightKey, @(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)height
{
    return objc_getAssociatedObject(self, heightKey);
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize newSize;

    if (self.height) {
        newSize = CGSizeMake(self.window.bounds.size.width, [self.height floatValue]);
    } else {
        newSize = [super sizeThatFits:size];
    }
    return newSize;
}

@end

The method [super sizeThatFits:size] was returning the wrong size so adding the following line after the super method will fix it.

newSize.width = self.window.bounds.size.width;

Seems that the storyboard views auto includes the category, thats why add it programatically works.

0
votes

I think you have created a extension of NavigationBar like below:

extension UINavigationBar {

    override public func sizeThatFits(size: CGSize) -> CGSize {
        super.sizeThatFits(size)
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = CGFloat(TABBAR_HEIGHT)
        //fix for fill to width of screen in new window
        if self.window != nil{
            sizeThatFits.width = self.window!.bounds.size.width
        }
        return sizeThatFits
    }
}