iosobjective-ciphoneiphone-xsafearealayoutguide

Calculate Height of UIView with Safe Area Guides for iPhone X


I have a UIView called headerview which I use like a NavigationBar. It has a constant size of 60 and top/left/right layout constraints which are 0. It looks okay for all devices except iPhone X because of safe area layout. If I change size to 90, It looks okay on iPhone X but now It looks wrong on other devices. I calculate height of safe area with below code and add it to height but now It looks small in other devices. What is the best approach to fix this problem?

    if (@available(iOS 11.0, *)) {
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    CGFloat topPadding = window.safeAreaInsets.top;
    self.headerViewHeight.constant = 46 + topPadding;
}

Solution

  • Try this one. Check the phone is iPhoneX or Not (Top padding greater than 0 if iPhoneX)

    if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.keyWindow;
        CGFloat topPadding = window.safeAreaInsets.top;
        self.headerViewHeight.constant = (topPadding>0) ? 46+topPadding : 60;
     }