iPhone X 的自动调整大小

AutoResizing for iPhone X

自动调整蒙版是否适用于 iPhoneX?

Apple 去年推出 What's New in Auto Layout 时,一切正常 没有任何限制

但是,当我在 iPhoneX 模拟器上尝试自动布局时,它不适用于 安全区域

(✓ 使用安全区域布局指南)

Auto Layout (without constraint)

With constraints

有没有其他方法如果有人找到,请在这里更新答案可能是我做错了

我尝试了一些东西,在你需要的地方定制你自己,这里我用在 viewcontroller ,代码是

根据你的看法在那个底部有一个按钮,这里我计算sa

例如

@property (strong, nonatomic) IBOutlet UIButton *btnKarthik;

#pragma mark - Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect modifyframe = self.btnKarthik.frame;
// here i changed the bottom origin Y position of my UIButton
modifyframe.origin.y = modifyframe.origin.y - [self getsafeAreaBottomMargin];
self.btnKarthik.frame = modifyframe;
}

常用的方法是

- (CGFloat) getsafeAreaBottomMargin {
if (@available(iOS 11.0, *)) {
    UIWindow *currentwindow = UIApplication.sharedApplication.windows.firstObject;
    return currentwindow.safeAreaLayoutGuide.owningView.frame.size.height - currentwindow.safeAreaLayoutGuide.layoutFrame.size.height - currentwindow.safeAreaLayoutGuide.layoutFrame.origin.y;
} else {
    return 0;
}
}

Swift3及以上

override func viewDidLoad() {
    super.viewDidLoad()

    var modifyframe: CGRect = btnKarthik.frame
    // here i changed the bottom origin Y position of my UIButton
    modifyframe.origin.y = modifyframe.origin.y - getsafeAreaBottomMargin()
    btnKarthik.frame = modifyframe
}

常用方法为

 func getsafeAreaBottomMargin() -> CGFloat {
   if #available(iOS 11.0, *) {
        let currentwindow = UIApplication.shared.windows.first
        return (currentwindow?.safeAreaLayoutGuide.owningView?.frame.size.height)! - (currentwindow?.safeAreaLayoutGuide.layoutFrame.size.height)! - (currentwindow?.safeAreaLayoutGuide.layoutFrame.origin.y)!
    }
    else {
        return 0
    }
}

iphone-X

的输出

其他iphone家族的产量

Move controls away from the edges on the iPhone X. Use the safe area layout guide and layout margins guide when creating constraints (use safeAreaIsets or layoutMargins if setting frames manually.

let margin = view.layoutMarginsGuide
NSLayoutConstraint.activate([
btnKarthik.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
btnKarthik.bottomAnchor.constraint(equalTo: margin.bottomAnchor)
])