10

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)

enter image description here

With constraints enter image description here

Willjay
  • 6,381
  • 4
  • 33
  • 58
  • And what is the problem? – jonaszmclaren Sep 27 '17 at 07:44
  • The location of the button would not be within safe area if I previously use auto layout (with no constraints). – Willjay Sep 27 '17 at 07:46
  • 1
    your ? is good. – Anbu.Karthik Sep 27 '17 at 07:55
  • Related questions about using auto layout with iPhone X and Safe Ares: https://stackoverflow.com/questions/46342023/bottom-margin-in-iphone-x-using-storyboard/46342821#46342821 and https://stackoverflow.com/questions/46299541/ios-11-calculate-safe-area-size/46301283#46301283 – jonaszmclaren Sep 27 '17 at 07:55
  • Well, specifying autoresizing masks is simple resize behavior for views with no constraints. So if this doesn't work in iPhoneX, I need to set up my views with constraints. – Willjay Sep 27 '17 at 08:02

1 Answers1

14

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

enter image description here

output of other iphone family

enter image description here

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)
])
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143