Objective C : How to create self.view inside Safe Area programmatically

半世苍凉 提交于 2019-11-27 11:59:04

问题


I have just changed my app from supporting iOS 8 and up to supporting iOS 9 and up.

Since I don't use storyboards to create my views, I was wondering if there's the "Use Safe Area Guides" option programmatically or something like that.

I've tried to anchor my view but they keep overlapping the top & bottom in the iPhone X simulator.


回答1:


Try this in Objective-C and see:

UIView * myView = // initialize view using IBOutlet or programtically

myView.backgroundColor = [UIColor redColor];
myView.translatesAutoresizingMaskIntoConstraints = NO;

if (@available(iOS 11, *)) {
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
    [myView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [myView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [myView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
    [myView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
} else {
    UILayoutGuide *margins = self.view.layoutMarginsGuide;
    [myView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
    [myView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
    [myView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
    [myView.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;

}

// Refresh myView and/or main view
[self.view layoutIfNeeded];
//[self.myView layoutIfNeeded];

Ref from: Use Safe Area Layout programmatically - Swift

Result:




回答2:


You can find top and bottom padding programmatically. I think this will solve your issue.

if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.keyWindow;
        CGFloat topPadding = window.safeAreaInsets.top;
        CGFloat bottomPadding = window.safeAreaInsets.bottom;
}

Edit: As mentioned in the comments (thanks, @albert-renshaw), the object can't be drawn from viewDidLoad on the first run as UIWindow won't be accessible until after at least one run loop. To bypass this you can do it several ways:

1. Simply move your viewDidLoad code into a new method postViewDidLoad and use:

[self performSelector:@selector(postViewDidLoad) withObject:nil afterDelay:0.0f];

...in the original viewDidLoad method, then UIWindow will be accessible.

OR

2. Enclose creation of your object in

dispatch_async(dispatch_get_main_queue(), ^{
// your code here...
});


来源:https://stackoverflow.com/questions/47075615/objective-c-how-to-create-self-view-inside-safe-area-programmatically

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