简体   繁体   English

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

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

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

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” Xcode文件检查器

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开始,点击警告将自动打开并突出显示冲突的标签”。我没有测试过这个)。

在此输入图像描述

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

在此输入图像描述

  1. Next, reveal your storyboard as source file. 接下来,将您的故事板显示为源文件。 在此输入图像描述

  2. 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. 顺便说一句,您可以通过选择项目并查看标识检查器,快速获取界面项的ID。 Very handy. 非常便利。

在此输入图像描述

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? 如何找到它?

  1. Go to Issue Navigator (CMD+8) and Select latest built with the warning 转到问题导航器 (CMD + 8)并选择最新构建的警告 在此输入图像描述
  2. Locate the warning(s) (search for " Automatic Preferred Max Layout ") and press expand button on the right 找到警告(搜索“ 自动首选最大布局 ”),然后按右侧的展开按钮
    在此输入图像描述
  3. Find the Object ID of the UILabel 找到 UILabel 的对象ID 在此输入图像描述
  4. Open the Storyboard and SEARCH (CMD+f) for the object. 打开Storyboard并搜索对象的SEARCH (CMD + f)。 It will SELECT AND HIGHLIGHT the UILabel 它将选择并突出显示UILabel
  5. Set Preferred Width = 0 "Explicit" as others suggested 如其他人所建议的那样设置首选宽度= 0“明确”

Solution it's quite simple 解决方案很简单

Just enable Builds for iOS 8 and Later 只需为iOS 8及更高版本启用Builds

在此输入图像描述

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;

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

Since I don't have a 50 reputation Stackoverflow wont let me comment on the second best answer. 由于我没有50的声誉Stackoverflow不会让我评论第二个最佳答案。 Found another trick for finding the culprit label in the Storyboard. 找到另一个在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,将直接带你到那个标签:)

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. 这是你应该按的地方。

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. 这是新的PreferredMaxWidth发挥作用的地方。 On iOS 7 and earlier you have to define the max width yourself. 在iOS 7及更早版本中,您必须自己定义最大宽度。 I simply added a 10px constraint to the left and right hand side of the label. 我只是在标签的左侧和右侧添加了一个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. 所以这实际上不是一个bug,你只需要自己定义最大宽度。 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. 我的上述解决方案确保无论父视图有多大,都始终保持宽度。

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文件也不会采用该更改并保留在“文件”检查器中的先前设置中

更改iOS部署目标后,Xib中的文件检查器

Therefore, you should change it manually for each Xib 因此,您应该为每个Xib手动更改它 手动更改

Once done, the warning will disappear :-) 完成后,警告将消失:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁止在8.0之前的iOS版本上使用“自动首选最大布局宽度”警告 - Inhibit `Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0` warnings Xcode 7中ios 8.0之前的自动首选最大布局宽度 - automatic preferred max layout width before ios 8.0 in Xcode 7 相对于8.0之前的iOS版本上的布局边距的布局属性 - Layout attributes relative to the layout margin on iOS versions prior to 8.0 属性不可用:8.0之前的iOS版本的第一个基线布局属性 - Attribute Unavailable: First baseline layout attribute on iOS versions prior to 8.0 8.0之前的iOS版本上的属性不可用警告 - Attribute Unavailable warning on iOS versions prior to 8.0 UIstoryboard:模态选择的“动画”选项在6.0之前的iOS版本上不可用 - UIstoryboard: Animates option for Modal segues is not available on iOS versions prior to 6.0 类不可用。 在6.0之前的iOS版本上无法使用取消同步功能 - Class Unavailable. Unwind segues are not available on iOS versions prior to 6.0 将应用程序升级到iOS 7 - 错误“iOS版本6.0之前的自动布局”(但希望iOS 7不旧) - Upgraded app to iOS 7 - error “Auto Layout on iOS Versions prior to 6.0” (but want iOS 7 not older) iOS版本6.0之前的UICollectionView - UICollectionView on iOS versions prior to 6.0 在iOS 8.0和更高版本中可用-为iOS 7构建 - Available in iOS 8.0 and later - build for iOS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM