xcode 目标 C:如何以编程方式在安全区域内创建 self.view
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47075615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C : How to create self.view inside Safe Area programmatically
提问by Ravindhiran
I have just changed my app from supporting iOS 8 and up to supporting iOS 9 and up.
我刚刚将我的应用程序从支持 iOS 8 及更高版本更改为支持 iOS 9 及更高版本。
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.
我试图锚定我的视图,但它们在 iPhone X 模拟器中的顶部和底部一直重叠。
回答by Krunal
Try this in Objective-C and see:
在 Objective-C 中试试这个,看看:
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:
结果:
回答by user6788419
You can find topand bottompadding 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 viewDidLoadon 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:
编辑:如评论中所述(感谢@albert-renshaw),在第一次运行时无法从viewDidLoad 中绘制对象,因为在至少一个运行循环之后才能访问 UIWindow。要绕过这一点,您可以通过多种方式做到这一点:
1.Simply move your viewDidLoad code into a new method postViewDidLoad and use:
1.只需将您的 viewDidLoad 代码移动到一个新方法 postViewDidLoad 并使用:
[self performSelector:@selector(postViewDidLoad) withObject:nil afterDelay:0.0f];
...in the original viewDidLoad method, then UIWindow will be accessible.
...在原来的 viewDidLoad 方法中,那么 UIWindow 将是可访问的。
OR
或者
2.Enclose creation of your object in
2.将对象的创建包含在
dispatch_async(dispatch_get_main_queue(), ^{
// your code here...
});