xcode iOS 11 之前的前导或尾随水平对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47456106/
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
leading or trailing horizontal alignment before iOS 11
提问by Prabhav
Warning: leading or trailing horizontal alignment before iOS 11
警告: leading or trailing horizontal alignment before iOS 11
I am getting above warning during compilation on Xcode 9.1 on one of the scenes in a storyboard file. There are other storyboards (with deployment target iOS 10.0)and yet the warning is shown to this specific scene on a specific Storyboard file.
在 Xcode 9.1 上的情节提要文件中的一个场景上进行编译时,我收到了以上警告。还有其他故事板(部署目标为 iOS 10.0),但警告显示在特定故事板文件中的特定场景中。
The warning gets suppressed if I change deployment target to iOS 11.0 on the scene where warning is shown but I don't want to do that.
如果我在显示警告的场景中将部署目标更改为 iOS 11.0,则警告会被抑制,但我不想这样做。
Has anyone come across this case?
有没有人遇到过这个案例?
回答by Grzegorz Krukowski
For me the problem was in using trailing
leading
alignment on UIButton
itself.
对我来说,问题在于trailing
leading
对UIButton
自身使用对齐方式。
Safe area seems to be completely fine to use - it's backward compatible and it translates into proper super view margins.
安全区域似乎完全可以使用 - 它是向后兼容的,并且可以转换为适当的超级视图边距。
But this feature is iOS 11 only, so use standard left / right alignment instead if you are targeting lower iOS versions.
但此功能仅适用于 iOS 11,因此如果您的目标是较低的 iOS 版本,请改用标准的左/右对齐方式。
Easiest way to find out which view is causing the problem is to search for contentHorizontalAlignment="leading"
or contentHorizontalAlignment="trailing"
in source code for .xib
找出导致问题的视图的最简单方法是搜索.xibcontentHorizontalAlignment="leading"
或contentHorizontalAlignment="trailing"
在源代码中
回答by etayluz
Step 1:
第1步:
View your offending storyboard as source code:
Step 2:
第2步:
Replace all instances of:
替换以下所有实例:
contentHorizontalAlignment="leading"
contentHorizontalAlignment="leading"
with:
和:
contentHorizontalAlignment="left"
contentHorizontalAlignment="left"
Step 3:
第 3 步:
Replace all instances of:
替换以下所有实例:
contentHorizontalAlignment="trailing"
contentHorizontalAlignment="trailing"
with:
和:
contentHorizontalAlignment="right"
contentHorizontalAlignment="right"
Step 4:
第四步:
Compile and watch warning disappear.
编译并观察警告消失。
I find this approach easier when you have a ton of elements that need to be modified. "leading" and "trailing" as 'contentHorizontalAlignment' value types were introduced with iOS 11. iOS 10 doesn't know about "leading" and "trailing" which is the reason for the warning.
当您有大量需要修改的元素时,我发现这种方法更容易。“领先”和“尾随”作为“contentHorizontalAlignment”值类型是在 iOS 11 中引入的。iOS 10 不知道“领先”和“尾随”是警告的原因。
回答by David Hoy
In the build log, right before the word "warning", you will see an Interface Builder identifier in the form "xxx-yy-zzz". Copy and paste that into the Xcode search bar, and it will find the "offending" control for you. Click on the search result and it will take you right into the storyboard with the corresponding control selected. The rest of the problem can be resolved using the other answers.
在构建日志中,就在“警告”一词之前,您将看到“xxx-yy-zzz”形式的 Interface Builder 标识符。将其复制并粘贴到 Xcode 搜索栏中,它会为您找到“违规”控件。单击搜索结果,它将带您直接进入故事板,并选择相应的控件。其余的问题可以使用其他答案解决。
回答by user462990
I had this problem with a whole bunch of buttons that I needed left aligned with a little offset. I removed the storyboard alignment and did it like this in viewDidLoad with an array of the needy buttons.
我在一堆按钮上遇到了这个问题,我需要左对齐并稍微偏移。我删除了故事板对齐方式,并在 viewDidLoad 中使用一组需要的按钮进行了操作。
func indentButtons(buttons: [UIButton?]){
for i in 0..<buttons.count{
buttons[i]!.contentHorizontalAlignment = .left
buttons[i]!.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0)
}
}