warning: Automatic Preferred Max Layout Width before iOS 8.0

解决方案地址:http://stackoverflow.com/questions/25398312/automatic-preferred-max-layout-width-is-not-available-on-ios-versions-prior-to-8


下面节选了几个好的解决方案的回答

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.

Steps to Reproduce:

  1. Create a new single-view, objective-c project 
  2. Set the Deployment Target to 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.
  6. Compile

I've filed the following radar:
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:

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.

Update 1:
This bug has now been fixed in Xcode 6 GM.

Original Answer
This is a bug in Xcode6-Beta6 and XCode6-Beta7 and can be safely ignored for now.

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. 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.

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" Xcode file inspector



14 
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.–  Charles A.  Oct 17 '14 at 1:31 
6 
Is there any way programatically to configure a UILabel to automatically adjust preferredMaxLayoutWidth, without actually subclassing it or implementing it somewhere in layoutSubviews? If this is a behavior that IB can enable, that suggests there's a property or magic value that can be set. But nothing in the API docs or headers says that. –  algal  Oct 17 '14 at 23:32
1 
@CharlesA. I've been testing with an empty project and found another label attribute that will trigger this warning: if numberOfLines is set to anything but 1, the warning will happen regardless of if you have preferredMaxLayoutWidth set or not. –  memmons  Oct 18 '14 at 15:44
2 
@MichaelG.Emmons Strange, I cannot reproduce this with a clean project and a deployment target set to 7.1. Once I set the preferredMaxLayoutWidth to an explicit 0 the warning goes away regardless of the numberOfLines value. But perhaps this was fixed in the Xcode update this morning? –  Charles A.  Oct 21 '14 at 18:01 
2 
So... is the only solution to set the number of lines to 1 and change it from code? this doesn't seem professional –  jomafer  Dec 4 '14 at 10:29


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". (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). 

enter image description here

  1. This will show the error with a code at the end of your storyboard file. Copy the value after .storyboard

enter image description here

  1. Next, reveal your storyboard as source file. enter image description here

  2. Search. You should be able to tell what label it is from here quite easily by looking at the content. enter image description here

Once you find the label the solution that worked for me was to set the "preferred width" to 0.

enter image description here

BTW, you can always quickly get the id of an interface item by selecting the item and looking under the identify inspector. Very handy.

enter image description here


34 
This should be the accepted answer! This actually solves the problem. It even helps find other issues in the Storyboard which don't show up normally. One thing: I find that the Report Navigator is much more reliable than right-clicking on the "Reveal in Log" –  Sam  Jan 19 '15 at 3:13
1 
Love this answer, very simple and fixed the warning. Thanks so much! –  Ethan Parker  Jan 19 '15 at 18:43
1 
thx a lot. your advice really help me –  sacred  Jan 22 '15 at 14:26
2 
Why do you set the preferred width to zero rather than to the width shown in interface builder? –  Biclops  Apr 8 '15 at 23:52
7 
Awesome answer! You can search for the object id directly from the IB view of the Storyboard. Just do a Ctrl-f. No need to open it as source code and then have to switch back to the IB view to change the preferred width. –  Alex  Jun 5 '15 at 20:51


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

HOW TO FIND IT?

  1. Go to Issue Navigator (CMD+8) and Select latest built with the warning enter image description here
  2. Locate the warning(s) (search for "Automatic Preferred Max Layout") and press expand button on the right
    enter image description here
  3. Find the Object ID of the UILabel enter image description here
  4. Open the Storyboard and SEARCH (CMD+f) for the object. It will SELECT AND HIGHLIGHTthe UILabel 
  5. Set Preferred Width = 0 "Explicit" as others suggested



Solution it's quite simple

Just enable Builds for iOS 8 and Later

enter image description here



quite simple and destructive.Only 85% of iOS users are on iOS8+ developer.apple.com/support/app-store –  aryaxt  Jul 29 '15 at 22:15 
6 
@aryaxt I am sorry to say you are wrong. You can set a deployment target in project settings to iOS 7 or lower and change Builds for in IB Documents to iOS 8 and later and it WILL work on iOS 7. Please verify it and remove the downvote. You can check it here developer.apple.com/library/ios/recipes/… –  Alejandro Luengo  Jul 30 '15 at 8:42
1 
Fantastic solution. Many thanks. –  tonyedwardspz  Aug 6 '15 at 12:07
1 
As always the best answer is not the accepted one... :) Cheers! –  HepaKKes  Oct 23 '15 at 9:32
 
UPDATE for iOS9.3 and up: iOS7 devices no longer render storyboards with the above setting. This used to work prior to the 9.3 update. Looks like Apple changed something in their pre-release automated process. –  pechar  Apr 14 at 11:56




还是stack Overflow上的大神比较多,上面的回答以及评论不仅解决了问题,还提供了一写其他错误调试的方法,太给力了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值