欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自动首选最大布局宽度在8.0之前的iOS版本上不可用

程序员文章站 2022-06-08 16:36:16
...

本文翻译自:Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0

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: 我尝试通过将宽度设置为显式来解决警告,如下所示:

自动首选最大布局宽度在8.0之前的iOS版本上不可用

Yet this didn't resolve the warnings. 然而,这并未解决警告。 How can they be removed? 他们怎么能被删除?


#1楼

参考:https://stackoom.com/question/1iZGC/自动首选最大布局宽度在-之前的iOS版本上不可用


#2楼

I got it working by selecting the original layout I had in the W / H selection. 我通过选择W / H选择中的原始布局来实现它。 Storyboard is working as expected and the error is gone. 故事板正在按预期工作,错误消失了。

Be also sure that you are developing for iOS 8.0. 还要确保您正在为iOS 8.0开发。 Check that from the project's general settings. 从项目的常规设置中检查。

This is where you should press. 这是你应该按的地方。


#3楼

Update 3: 更新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. 如果部署目标设置为7.1,则numberOfLines设置为除1之外的任何标签的标签也可以触发此警告。 This is completely reproducible with new single-view project. 这可以通过新的单视图项目完全重现。

Steps to Reproduce: 重现步骤:

  1. Create a new single-view, objective-c project 创建一个新的单视图,objective-c项目
  2. Set the Deployment Target to 7.1 将部署目标设置为7.1
  3. Open the project's storyboard 打开项目的故事板
  4. Drop a label onto the provided view controller 将标签拖放到提供的视图控制器上
  5. Set the numberOfLines for that label to 2. 将该标签的numberOfLines设置为2。
  6. Compile

I've filed the following radar: 我提交了以下雷达:
rdar://problem/18700567 rdar://问题/ 18700567

Update 2: 更新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. 不幸的是,在Xcode 6的发布版本中再次出现这种情况。请注意,您可以在大多数情况下手动编辑storyboard / xib来解决问题。 Per Charles A. in the comments below: Per 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. 您可以将故事板作为源文件打开,并使用regex <label(?!.*preferredMaxLayoutWidth)进行搜索,以查找省略preferredMaxLayoutWidth属性/值的标签。 If you add in preferredMaxLayoutWidth="0" on such lines, it is the same as marking explicit and setting the value 0. 如果在这些行上添加preferredMaxLayoutWidth =“0”,则与标记显式和设置值0相同。

Update 1: 更新1:
This bug has now been fixed in Xcode 6 GM. 此错误现已在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 this to say about the bug: 苹果开发者论坛上的一位苹果工程师对此漏洞说:

Preferred max layout width is an auto layout property on UILabel that allows it to automatically grow vertically to fit its content. 首选的最大布局宽度是UILabel上的自动布局属性,允许它自动垂直增长以适合其内容。 Versions of Xcode prior to 6.0 would set preferredMaxLayoutWidth for multiline labels to the current bounds size at design time. 6.0之前的Xcode版本会在设计时将多行标签的preferredMaxLayoutWidth设置为当前边界大小。 You would need to manually update preferredMaxLayoutWidth at runtime if your horizontal layout changed. 如果水平布局发生更改,则需要在运行时手动更新preferredMaxLayoutWidth。

iOS 8 added support for automatically computing preferredMaxLayoutWidth at runtime, which makes creating multiline labels even easier. iOS 8增加了对在运行时自动计算preferredMaxLayoutWidth的支持,这使得创建多行标签变得更加容易。 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. 此设置不向后兼容iOS 7.为了支持iOS 7和iOS 8,Xcode 6允许您在尺寸检查器中为preferredMaxLayoutWidth选择“自动”或“显式”。 You should: 你应该:

Pick "Automatic" if targeting iOS 8 for the best experience. 如果以iOS 8为目标,请选择“自动”以获得最佳体验。 Pick "Explicit" if targeting < iOS 8. You can then enter the value of preferredMaxLayoutWidth you would like set. 如果定位<iOS 8,请选择“明确”。然后,您可以输入要设置的preferredMaxLayoutWidth的值。 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. 如果(1)您正在使用自动布局,则会出现警告,(2)为多行标签设置“自动”[您可以在尺寸检查器中检查标签],以及(3)您的部署目标<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. 如果您看到此警告但未使用自动布局,则可以忽略该警告。

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上的文件检查器来解决此问题,并将“Build for for”更改为“Build for iOS 8.0 and Later” 自动首选最大布局宽度在8.0之前的iOS版本上不可用


#4楼

Now my Xcode version is 6.1. 现在我的Xcode版本是6.1。 But I got this warning too. 但我也得到了这个警告。 it annoys me a lot . 它让我很烦恼。 after search again and again.I found the solution. 经过一次又一次的搜索。我找到了解决方案。

Reason:You must have set your UILabel Lines > 1 in your Storyboard. 原因:您必须在故事板中设置UILabel线> 1。

Solution: set your UILabel Lines attribute to 1 in Storyboard. 解决方案:在Storyboard中将UILabel Lines属性设置为1。 restart your Xcode. 重启你的Xcode。 It works for me, hope it can help more people. 它适用于我,希望它可以帮助更多的人。

If you really need to show your words more than 1 line. 如果你真的需要显示超过1行的单词。 you should do it in the code. 你应该在代码中做到这一点。

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

#5楼

总而言之,对于我,按照上面的两条说明将numberOfLines = 0更改为1或更大的任何实例,并手动将optimMaxLayoutWidth =“0”添加到storyboard源内的每个标签实例,修复了我的所有警告。


#6楼

To Find the problem label(s) in a large storyboard, follow my steps below. 要在大型故事板中查找问题标签,请按照以下步骤操作。

  1. In xCode's Issue Navigator right click on the error and select "Reveal In Log". 在xCode的Issue Navigator中右键单击错误并选择“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). (注意:@Sam建议在下面,查看xCode的报告导航器 。另外@Rivera在评论中注意到“从Xcode 6.1.1开始,点击警告将自动打开并突出显示冲突的标签”。我没有测试过这个)。

自动首选最大布局宽度在8.0之前的iOS版本上不可用

  1. This will show the error with a code at the end of your storyboard file. 这将显示故事板文件末尾的代码错误。 Copy the value after .storyboard 复制.storyboard之后的值

自动首选最大布局宽度在8.0之前的iOS版本上不可用

  1. Next, reveal your storyboard as source file. 接下来,将您的故事板显示为源文件。 自动首选最大布局宽度在8.0之前的iOS版本上不可用

  2. Search. 搜索。 You should be able to tell what label it is from here quite easily by looking at the content. 你应该能够通过查看内容很容易地从这里告诉它是什么标签。 自动首选最大布局宽度在8.0之前的iOS版本上不可用

Once you find the label the solution that worked for me was to set the "preferred width" to 0. 一旦找到标签,对我有用的解决方案是将“首选宽度”设置为0。

自动首选最大布局宽度在8.0之前的iOS版本上不可用

BTW, you can always quickly get the id of an interface item by selecting the item and looking under the identify inspector. 顺便说一句,您可以通过选择项目并查看标识检查器,快速获取界面项的ID。 Very handy. 非常便利。

自动首选最大布局宽度在8.0之前的iOS版本上不可用