本文介绍了如何以编程方式添加安全区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
打开视图时,它看起来像下面的图片,
When you open the view, it will look like the image below,
对于iPhone X,我想以编程方式在当前视图中添加一个安全区域.
For iphone x, I would like to add a safe area programmatically in the current view.
尝试的来源如下.
UIView *view = self.view;
if (@available(iOS 11.0, *)) {
UILayoutGuide * guide = view.safeAreaLayoutGuide;
[view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
}
我想应用此资源,但我不知道该怎么办.
I suppose to apply this source, but I do not know what to do.
请回答我的问题!!
推荐答案
此处是安全区域布局"的示例代码.在Objective-C中尝试此操作,请参见:
Here is sample code for Safe Area Layout. Try this in Objective-C and see:
UIView * myView = // initialize view using IBOutlet or programtically
myView.backgroundColor = [UIColor red];
myView.translatesAutoresizingMaskIntoConstraints = NO;
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[self.myView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[self.myView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[self.myView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[self.myView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
// Refresh myView and/or main view
[self.view layoutIfNeeded];
//[self.myView layoutIfNeeded];
引用自:以编程方式使用安全区域布局
结果:
这篇关于如何以编程方式添加安全区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!