iOS 11 navigation bar height customizing

后端 未结 11 888
无人共我
无人共我 2020-11-28 01:27

Now in iOS 11, the sizeThatFits method is not called from UINavigationBar subclasses. Changing the frame of UINavigationBar causes gli

11条回答
  •  臣服心动
    2020-11-28 01:53

    Added: The problem is solved in iOS 11 beta 6 ,so the code below is of no use ^_^


    Original answer:

    Solved with code below :

    (I always want the navigationBar.height + statusBar.height == 64 whether the hidden of statusBar is true or not)

     @implementation P1AlwaysBigNavigationBar
    
    - (CGSize)sizeThatFits:(CGSize)size {
        CGSize sizeThatFit = [super sizeThatFits:size];
        if ([UIApplication sharedApplication].isStatusBarHidden) {
            if (sizeThatFit.height < 64.f) {
                sizeThatFit.height = 64.f;
            }
        }
        return sizeThatFit;
    }
    
    - (void)setFrame:(CGRect)frame {
        if ([UIApplication sharedApplication].isStatusBarHidden) {
            frame.size.height = 64;
        }
        [super setFrame:frame];
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        if (![UIApplication sharedApplication].isStatusBarHidden) {
            return;
        }
    
        for (UIView *subview in self.subviews) {
            NSString* subViewClassName = NSStringFromClass([subview class]);
            if ([subViewClassName containsString:@"UIBarBackground"]) {
                subview.frame = self.bounds;
            }else if ([subViewClassName containsString:@"UINavigationBarContentView"]) {
                if (subview.height < 64) {
                    subview.y = 64 - subview.height;
                }else {
                    subview.y = 0;
                }
            }
        }
    }
    @end
    

提交回复
热议问题