【发布时间】:2020-04-10 23:40:57
【问题描述】:
我是 iOS 应用程序开发的新手,目前正在开发使用 Objective-C 编写的现有 iOS 应用程序。我遇到了一个需要以编程方式实现安全区域的要求。所以我在“viewDidLoad”方法中实现了以下代码。
if(@available(iOS 11, *)){
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[self.view.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[self.view.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[self.view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[self.view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
}else{
UILayoutGuide * margins = self.view.layoutMarginsGuide;
[self.view.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
[self.view.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
[self.view.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
[self.view.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;
}
但是,当我尝试运行连接到 iPhone XR iOS 模拟器设备的应用程序时,上面的代码不起作用。在横向模式下的安全区域之后视图没有开始,而是从安全区域本身开始,因此文本被截断
我错过了什么吗?您能否建议需要做什么才能实施安全区域?
【问题讨论】:
-
尝试在 viewDidLayoutSubviews 中应用约束
-
在您的代码中,没有文本的约束。请添加。
-
之前我尝试为 UILabel 设置约束,但由于控件从显示区域移动,标签宽度正在增加。只有标签内容会显示在整个屏幕中。所以我已经将约束应用于视图。
标签: ios objective-c xcode11 safearealayoutguide