AutoResizing for iPhone X

旧巷老猫 提交于 2019-12-17 16:10:48

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/46441988/autoresizing-for-iphone-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!