适用于iPhone X的AutoResizing [英] AutoResizing for iPhone X

查看:173
本文介绍了适用于iPhone X的AutoResizing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

autoresizing mask是否适用于iPhoneX?



当Apple推出

解决方案

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



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



在你的视图底部有一个按钮,在这里我计算sa



例如

  @property(强,非原子)IBOutlet UIButton * btnKarthik; 

#pragma mark - 生命周期
- (无效)viewDidLoad
{
[super viewDidLoad];
CGRect modifyframe = self.btnKarthik.frame;
//这里我更改了我的UIButton的底部原点Y位置
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;
返回currentwindow.safeAreaLayoutGuide.owningView.frame.size.height - currentwindow.safeAreaLayoutGuide.layoutFrame.size.height - currentwindow.safeAreaLayoutGuide.layoutFrame.origin.y;
} else {
返回0;
}
}

Swift3及以上

 覆盖func viewDidLoad(){
super.viewDidLoad()

var modifyframe:CGRect = btnKarthik.frame
//这里我更改了我的UIButton的底部原点Y位置
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 {
返回0
}
}

iphone-X的输出





其他iphone系列的输出




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




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


Does autoresizing masks work on iPhoneX?

When Apple introduced What's New in Auto Layout last year, everything works fine with no constraints.

However, when I try auto layout on iPhoneX simulator, it doesn't work for the safe area.

(✓ Use Safe Area Layout Guides)

Auto Layout (without constraint)

With constraints

解决方案

is there any alternate way if anyone found, please update the answer here may be I did wrong

I tried something and customize yourself where you need, in here I used in viewcontroller , the code is

on that bottom based on your view has one button , in here I calculate the sa

For Example

@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;
}

the common method is

- (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 and above

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
}

common method as

 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
    }
}

output of iphone-X

output of other iphone family

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)
])

这篇关于适用于iPhone X的AutoResizing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆