我用Xcode6 beta6打开了一个现有的iOS项目,Xcode列出了以下对Storyboard和Xib文件的警告:

自动首选最大布局宽度在iOS版本中不可用 8.0以前版本

我尝试通过设置宽度来解决警告,如下所示:

然而,这并没有消除警告。如何去除?


我通过选择W / H选项中的原始布局来让它工作。故事板正在正常工作,错误已经消失。

还要确保你开发的是iOS 8.0。从项目的一般设置中检查。

这就是你应该按的地方。

更新3: 如果部署目标设置为7.1,那么numberolines设置为1以外的任何值的标签也可能触发此警告。这是完全可复制的与新的单视图项目。

复制步骤:

创建一个新的单视图objective-c项目 将部署目标设置为7.1 打开项目的故事板 将标签放到所提供的视图控制器上 将该标签的numberolines设置为2。 编译

我已经提交了以下雷达: rdar: / /问题/ 18700567

更新2: 不幸的是,在Xcode 6的发行版中又出现了这种情况。请注意,在大多数情况下,您可以手动编辑故事板/xib来修复这个问题。以下是查尔斯·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.

更新1: 这个错误现在已经在Xcode 6 GM中修复。

原来的答案 这是Xcode6-Beta6和XCode6-Beta7中的一个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.

或者,你可以通过使用故事板或xib上的文件检查器来解决这个问题,并将“用于”更改为“用于iOS 8.0及以后版本的构建”

现在我的Xcode版本是6.1。但我也收到了警告。这让我很恼火。经过一遍又一遍的搜索。我找到了解决办法。

原因:你必须在你的故事板中设置你的UILabel Lines > 1。

解决方案:在Storyboard中将UILabel Lines属性设置为1。重启Xcode。这对我有用,希望它能帮助更多的人。

如果你确实需要显示你的单词超过一行。你应该在代码里写。

//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;

总而言之,对于我来说,按照上面的两条指令来更改numberOfLines = 0为1或更大的任何实例,并手动将preferredmaxlayoutidth ="0"添加到故事板源代码中的每个标签实例中,可以修复我的所有警告。

要在大型故事板中找到问题标签,请遵循以下步骤。

在xCode的Issue Navigator中右键单击错误并选择“Reveal In Log”。(注:@Sam的建议如下,看看xCode的报表导航器。此外,@Rivera在评论中指出,“从Xcode 6.1.1开始,点击警告将自动打开并突出显示冲突标签”。我还没有对此进行测试)。

这将在故事板文件的末尾用代码显示错误。复制.storyboard后的值

接下来,将故事板显示为源文件。 搜索。通过查看内容,您应该能够很容易地从这里判断出它是什么标签。

找到标签后,对我来说有效的解决方案是将“首选宽度”设置为0。

顺便说一句,你总是可以通过选择项目并在标识检查器下查看来快速获得接口项目的id。非常方便。

因为我没有50的声誉,Stackoverflow不会让我评论第二好的答案。发现了在Storyboard中寻找罪犯标签的另一个技巧。

因此,一旦你知道了标签的id,在一个单独的选项卡中打开你的故事板,显示视图控制器,只需执行F命令和V命令,就会直接带你到那个标签:)

我有这个问题,并能够通过添加约束来确定标签的最大值来修复它。

当删除多行标签时,没有约束设置来强制父视图内的宽度。这就是新的PreferredMaxWidth开始发挥作用的地方。在iOS 7和更早的版本上,你必须自己定义最大宽度。我只是在标签的左右两侧添加了一个10px的约束。

您还可以添加<= width约束,这也可以解决这个问题。

所以这实际上不是一个bug,你只需要自己定义最大宽度。在其他答案中提到的显式选项也可以工作,因为你正在设置这个宽度值,但是如果你想要根据父宽度改变最大宽度,你必须修改这个值(因为你已经显式地设置了宽度)。

我上面的解决方案确保了无论父视图有多大,宽度总是保持不变。

您可以在不将故事板作为源打开的情况下修复此问题。 如果numberolines !=1并且部署目标< 8.0,UILabels将触发此警告

如何找到它?

转到Issue Navigator (CMD+8)并选择带有警告的最新构建 找到警告(s)(搜索“自动首选最大布局”)并按下右边的展开按钮 找到UILabel的对象ID 打开Storyboard并搜索(CMD+f)对象。它将选择并突出显示UILabel 根据其他人的建议,设置Preferred Width = 0 "Explicit"

解决方法很简单

只需要启用iOS 8及更高版本的构建即可

出于某种原因,即使将iOS部署目标更改为8.0或更高版本,Xib文件也不会采用该更改,并在文件检查器中保持先前的设置

因此,您应该为每个Xib手动更改它

一旦完成,警告将消失:-)