自动首选最大布局宽度在 8.0 之前的 iOS 版本上不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25398312/
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
Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
提问by mono
I opened an existing iOS project with Xcode6 beta6, and Xcode lists the following warning for both Storyboard and Xib files:
我用 Xcode6 beta6 打开了一个现有的 iOS 项目,Xcode 为 Storyboard 和 Xib 文件列出了以下警告:
Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
自动首选最大布局宽度在 8.0 之前的 iOS 版本上不可用
I tried addressing the warning by setting the width as explicit like below:
我尝试通过将宽度设置为如下所示来解决警告:
Yet this didn't resolve the warnings. How can they be removed?
然而,这并没有解决警告。如何去除它们?
回答by memmons
Update 3:
This warning can also be triggered by labels that have numberOfLines
set to anything but 1 if your deployment target is set to 7.1. This is completely reproducible with new single-view project.
更新 3:如果您的部署目标设置为 7.1
,则此警告也可能由numberOfLines
设置为 1 以外的任何标签触发。这完全可以通过新的单视图项目重现。
Steps to Reproduce:
重现步骤:
- Create a new single-view, objective-c project
- Set the Deployment Target to 7.1
- Open the project's storyboard
- Drop a label onto the provided view controller
- Set the numberOfLines for that label to 2.
- Compile
- 创建一个新的单视图、objective-c 项目
- 将部署目标设置为 7.1
- 打开项目的故事板
- 将标签拖放到提供的视图控制器上
- 将该标签的 numberOfLines 设置为 2。
- 编译
I've filed the following radar:
rdar://problem/18700567
我已经提交了以下雷达:
rdar://problem/18700567
Update 2:
Unfortunately, this is a thing again in the release version of Xcode 6. Note that you can, for the most part, manually edit your storyboard/xib to fix the problem. Per Charles A.in the comments below:
更新 2:
不幸的是,这在 Xcode 6 的发布版本中再次出现。请注意,在大多数情况下,您可以手动编辑您的故事板/xib 来解决问题。根据Charles A.在下面的评论中:
It's worth mentioning that you can pretty easily accidentally introduce this warning, and the warning itself doesn't help in finding the label that is the culprit. This is unfortunate in a complex storyboard. You can open the storyboard as a source file and search with the regex
<label(?!.*preferredMaxLayoutWidth)
to find labels that omit a preferredMaxLayoutWidth attribute/value. If you add in preferredMaxLayoutWidth="0" on such lines, it is the same as marking explicit and setting the value 0.
值得一提的是,您很容易意外地引入此警告,并且警告本身无助于找到罪魁祸首的标签。这在复杂的故事板中是不幸的。您可以将情节提要作为源文件打开并使用正则表达式
<label(?!.*preferredMaxLayoutWidth)
进行搜索以查找省略 preferredMaxLayoutWidth 属性/值的标签。如果在这些行中添加 preferredMaxLayoutWidth="0" ,则与标记为显式并设置值为 0 相同。
Update 1:
This bug has now been fixed in Xcode 6 GM.
更新 1:
此错误现已在 Xcode 6 GM 中修复。
Original Answer
This is a bug in Xcode6-Beta6 and XCode6-Beta7 and can be safely ignored for now.
原始答案
这是 Xcode6-Beta6 和 XCode6-Beta7 中的一个错误,现在可以安全地忽略。
An Apple engineer in the Apple Developer forums had thisto say about the bug:
Apple 开发者论坛中的一位 Apple 工程师对这个 bug有这样的看法:
Preferred max layout width is an auto layout property on UILabel that allows it to automatically grow vertically to fit its content. Versions of Xcode prior to 6.0 would set preferredMaxLayoutWidth for multiline labels to the current bounds size at design time. You would need to manually update preferredMaxLayoutWidth at runtime if your horizontal layout changed.
iOS 8 added support for automatically computing preferredMaxLayoutWidth at runtime, which makes creating multiline labels even easier. This setting is not backwards compatible with iOS 7. To support both iOS 7 and iOS 8, Xcode 6 allows you to pick either "Automatic" or "Explicit" for preferredMaxLayoutWidth in the size inspector. You should:
Pick "Automatic" if targeting iOS 8 for the best experience. Pick "Explicit" if targeting < iOS 8. You can then enter the value of preferredMaxLayoutWidth you would like set. Enabling "Explicit" defaults to the current bounds size at the time you checked the box.
The warning will appear if (1) you're using auto layout, (2) "Automatic" is set for a multiline label [you can check this in the size inspector for the label], and (3) your deployment target < iOS 8.
It seems the bug is that this warning appears for non-autolayout documents. If you are seeing this warning and not using auto layout you can ignore the warning.
首选的最大布局宽度是 UILabel 上的一个自动布局属性,允许它自动垂直增长以适应其内容。Xcode 6.0 之前的版本会在设计时将多行标签的 preferredMaxLayoutWidth 设置为当前边界大小。如果水平布局发生变化,您将需要在运行时手动更新 preferredMaxLayoutWidth。
iOS 8 添加了在运行时自动计算 preferredMaxLayoutWidth 的支持,这使得创建多行标签变得更加容易。此设置不向后兼容 iOS 7。为了支持 iOS 7 和 iOS 8,Xcode 6 允许您在大小检查器中为 preferredMaxLayoutWidth 选择“自动”或“显式”。你应该:
如果针对 iOS 8 以获得最佳体验,请选择“自动”。如果目标是 < iOS 8,请选择“Explicit”。然后您可以输入要设置的 preferredMaxLayoutWidth 的值。启用“显式”默认为您选中该框时的当前边界大小。
如果 (1) 您正在使用自动布局,(2) 为多行标签设置了“自动”[您可以在标签的大小检查器中进行检查],以及 (3) 您的部署目标 < iOS,则会出现警告8.
似乎错误是此警告出现在非自动布局文档中。如果您看到此警告且未使用自动布局,则可以忽略该警告。
Alternately, you can work around the issue by using the file inspector on the storyboard or xib in question and change "Builds for" to "Builds for iOS 8.0 and Later"
或者,您可以通过在故事板或 xib 上使用文件检查器来解决该问题,并将“Builds for”更改为“Builds for iOS 8.0 and later”
回答by smileBot
To Find the problem label(s) in a large storyboard, follow my steps below.
要在大型故事板中查找问题标签,请按照以下步骤操作。
- In xCode's Issue Navigator right click on the error and select "Reveal In Log". (Note: @Sam suggests below, look in xCode's report navigator. Also @Rivera notes in the comments that "As of Xcode 6.1.1, clicking on the warning will automatically open and highlight the conflicting label". I haven't tested this).
- 在 xCode 的问题导航器中,右键单击错误并选择“Reveal In Log”。(注意:@Sam 在下面建议,查看 xCode 的报告导航器。@Rivera 在评论中也指出“从 Xcode 6.1.1 开始,单击警告将自动打开并突出显示冲突的标签”。我还没有测试过这个)。
- This will show the error with a code at the end of your storyboard file. Copy the value after .storyboard
- 这将在故事板文件的末尾显示带有代码的错误。复制 .storyboard 后的值
Next, reveal your storyboard as source file.
Search. You should be able to tell what label it is from here quite easily by looking at the content.
接下来,将您的故事板显示为源文件。
搜索。通过查看内容,您应该能够从这里很容易地分辨出它是什么标签。
Once you find the label the solution that worked for me was to set the "preferred width" to 0.
一旦找到标签,对我有用的解决方案就是将“首选宽度”设置为 0。
BTW, you can always quickly get the id of an interface item by selecting the item and looking under the identify inspector. Very handy.
顺便说一句,您总是可以通过选择项目并在识别检查器下查看来快速获取界面项目的 id。非常便利。
回答by Tibidabo
You can fix this issue without opening the storyboard as a source. This warning is triggered by UILabels if numberOfLines !=1 and deployment target is < 8.0
您可以在不打开故事板作为源的情况下解决此问题。如果 numberOfLines !=1 并且部署目标 < 8.0,则此警告由 UILabels 触发
HOW TO FIND IT?
如何找到它?
- Go to Issue Navigator(CMD+8) and Select latest built with the warning
- Locate the warning(s)(search for "Automatic Preferred Max Layout") and press expand button on the right
- Find the Object IDof the UILabel
- Open the Storyboard and SEARCH(CMD+f) for the object. It will SELECT AND HIGHLIGHTthe UILabel
- Set Preferred Width = 0 "Explicit"as others suggested
- 转到问题导航器(CMD + 8)并选择带有警告的最新构建
- 找到警告(搜索“自动首选最大布局”),然后按右侧的展开按钮
- 找到UILabel
的对象 ID
- 打开对象的 Storyboard 和SEARCH(CMD+f)。它将选择并突出显示 UILabel
- 按照其他人的建议设置首选宽度 = 0“显式”
回答by Alejandro Luengo
Solution it's quite simple
解决方法很简单
Just enable Builds for iOS 8 and Later
只需为 iOS 8 及更高版本启用 Builds
回答by ronan
Now my Xcode version is 6.1. But I got this warning too. it annoys me a lot . after search again and again.I found the solution.
现在我的 Xcode 版本是 6.1。但我也收到了这个警告。这让我很恼火。经过一遍又一遍的搜索,我找到了解决方案。
Reason:You must have set your UILabel Lines > 1 in your Storyboard.
原因:您必须在 Storyboard 中设置 UILabel Lines > 1。
Solution: set your UILabel Lines attribute to 1 in Storyboard. restart your Xcode. It works for me, hope it can help more people.
解决方案:在 Storyboard 中将 UILabel Lines 属性设置为 1。重新启动您的 Xcode。它对我有用,希望它可以帮助更多的人。
If you really need to show your words more than 1 line. you should do it in the code.
如果您真的需要显示超过 1 行的话。你应该在代码中做到这一点。
//the words will show in UILabel
NSString *testString = @"Today I wanna set the line to multiple lines. bla bla ...... Today I wanna set the line to multiple lines. bla bla ......"
[self.UserNameLabel setNumberOfLines:0];
self.UserNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
UIFont *font = [UIFont systemFontOfSize:12];
//Here I set the Label max width to 200, height to 60
CGSize size = CGSizeMake(200, 60);
CGRect labelRect = [testString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];
self.UserNameLabel.frame = CGRectMake(self.UserNameLabel.frame.origin.x, self.UserNameLabel.frame.origin.y, labelRect.size.width, labelRect.size.height);
self.UserNameLabel.text = testString;
回答by Lynn S
To summarize, for me following the two instructions above to change any instances where numberOfLines = 0 to 1 or greater, and manually adding preferredMaxLayoutWidth="0" to each instance of a label inside the storyboard source fixed all of my warnings.
总而言之,对我来说,按照上面的两条说明将 numberOfLines = 0 的任何实例更改为 1 或更大,并手动将 preferredMaxLayoutWidth="0" 添加到故事板源中的每个标签实例修复了我的所有警告。
回答by Junaid Ahmed
Since I don't have a 50 reputation Stackoverflow wont let me comment on the second best answer. Found another trick for finding the culprit label in the Storyboard.
由于我没有 50 声望,Stackoverflow 不会让我评论第二个最佳答案。找到了在 Storyboard 中找到罪魁祸首标签的另一个技巧。
So once you know the id of the label, open your storyboard in a seperate tab with view controllers displayed and just do command F and command V and will take you straight to that label :)
因此,一旦您知道标签的 id,请在显示视图控制器的单独选项卡中打开您的故事板,然后执行命令 F 和命令 V 并直接将您带到该标签:)
回答by AarneHuttunen
I got it working by selecting the original layout I had in the W / H selection. Storyboard is working as expected and the error is gone.
我通过选择 W / H 选择中的原始布局来使其工作。故事板按预期工作,错误消失了。
Be also sure that you are developing for iOS 8.0. Check that from the project's general settings.
还要确保您正在为 iOS 8.0 进行开发。从项目的常规设置中检查。
回答by Matthew Cawley
I had this issue and was able to fix it by adding constraints to determine the max with for a label.
我遇到了这个问题,并且能够通过添加约束来确定标签的最大值来解决它。
When dropping a multiline label in there is not constraint set to enforce the width inside the parent view. This is where the new PreferredMaxWidth comes into play. On iOS 7 and earlier you have to define the max width yourself. I simply added a 10px constraint to the left and right hand side of the label.
在放置多行标签时,没有设置约束来强制父视图内的宽度。这就是新的 PreferredMaxWidth 发挥作用的地方。在 iOS 7 及更早版本上,您必须自己定义最大宽度。我只是在标签的左侧和右侧添加了一个 10px 的约束。
You can also add a <= width constraint which also fixes the issue.
您还可以添加一个 <= 宽度约束,这也解决了这个问题。
So this is not actually a bug, you simply have to define the max width yourself. The explicit option mention in other answer will also work as you are setting this width value however you will have to modify this value if you want the max width to change based on the parent width (as you have explicitly set the width).
所以这实际上不是一个错误,你只需要自己定义最大宽度。其他答案中提到的显式选项也可以在您设置此宽度值时起作用,但是如果您希望最大宽度根据父宽度更改(因为您已明确设置宽度),则必须修改此值。
My above solution ensures the width is always maintained no matter how big the parent view is.
我的上述解决方案确保无论父视图有多大,宽度始终保持不变。
回答by Gutty1
For some reason, even if changing the iOS Deployment Target to 8.0 or higher, the Xib files don't adopt that change and remain with the previous settings in the File inspector
出于某种原因,即使将 iOS 部署目标更改为 8.0 或更高版本,Xib 文件也不会采用该更改并保留文件检查器中先前的设置
Therefore, you should change it manually for each Xib
Once done, the warning will disappear :-)
完成后,警告将消失:-)