uinavigationbarios11objective-c-category

iOS 11 UINavigationBar category issue


When I override layoutSubviews of UINavigationbar, all the subviews are getting removed. I am trying to set custom Navigationbar height. It was working fine, but breaking in iOS 11. It is a custom category.


Solution

  • I have created a subclass of UINavigationBar with setFrame: and layoutSubviews methods. You need to set the subclass as NavigationController's NavigationBar in Storyboard.

    CGFloat const NavBarHeight = 88.0;
    
    - (CGSize)sizeThatFits:(CGSize)size {
        return [self getNavBarSize];
    }
    
    - (void)setFrame:(CGRect)frame {
        frame.size.height = NavBarHeight;
    
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class])  containsString:@"UINavigationBarContentView"]) {
                subview.frame = CGRectMake(0, 20, self.frame.size.width, NavBarHeight-30);
            }
            [subview sizeToFit];
        }
        [super setFrame:frame];
    
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) containsString:@"BarBackground"]) {
                subview.frame = CGRectMake(0, -20, self.frame.size.width, NavBarHeight+20);
                [subview sizeToFit];
            }else if ([NSStringFromClass([subview class])  containsString:@"UINavigationBarContentView"]) {
                subview.frame = CGRectMake(0, 20, self.frame.size.width, NavBarHeight-30);
            }
        }
    }