autoresizing mask是否适用于iPhoneX?
去年Apple推出What's New in Auto Layout时,一切正常没有约束。
但是,当我在iPhoneX模拟器上尝试自动布局时,它不适用于安全区域。
(✓使用安全区域布局指南)
Auto Layout (without constraint)
答案 0 :(得分: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;
}
常用方法是
- (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系列的输出
将控件移离iPhone X上的边缘。在创建约束时使用安全区域布局指南和布局边距指南(如果手动设置框架,请使用safeAreaIsets或layoutMargins。
let margin = view.layoutMarginsGuide
NSLayoutConstraint.activate([
btnKarthik.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
btnKarthik.bottomAnchor.constraint(equalTo: margin.bottomAnchor)
])