适用于iPhone X的AutoResizing

Wil*_*jay 9 ios iphone-x

自动调整掩码是否适用于iPhoneX?

当Apple 去年推出Auto Layout的新功能时,一切正常,没有任何限制.

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

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

Auto Layout (without constraint)

在此输入图像描述

With constraints 在此输入图像描述

Anb*_*hik 14

如果有人发现有任何替代方式,请在这里更新答案可能是我做错了

我尝试了一些东西并在你需要的地方自定义,在这里我在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;
}
Run Code Online (Sandbox Code Playgroud)

常用的方法是

- (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;
}
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

常用方法如

 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
    }
}
Run Code Online (Sandbox Code Playgroud)

iphone-X的输出

在此输入图像描述

其他iphone系列的输出

在此输入图像描述

将控件移离iPhone X上的边缘.在创建约束时使用安全区域布局指南和布局边距指南(如果手动设置框架,请使用safeAreaIsets或layoutMargins.

let margin = view.layoutMarginsGuide
NSLayoutConstraint.activate([
btnKarthik.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
btnKarthik.bottomAnchor.constraint(equalTo: margin.bottomAnchor)
])
Run Code Online (Sandbox Code Playgroud)