简体   繁体   English

相对于8.0之前的iOS版本上的布局边距的布局属性

[英]Layout attributes relative to the layout margin on iOS versions prior to 8.0

What would be causing the following warning (and subsequent alignment issues on iOS 7)? 什么会导致以下警告(以及iOS 7上的后续对齐问题)?

Attribute Unavailable: Layout attributes relative to the layout margin on iOS versions prior to 8.0 属性不可用:相对于8.0之前的iOS版本上的布局边距的布局属性

None of the posted answers solved the problem for me. 发布的答案都没有为我解决问题。 But the reason for this is the following: Xcode 6 creates constraints based on relative margins by default. 但原因如下: 默认情况下,Xcode 6会根据相对边距创建约束。 Those are only available on iOS 8.0 and newer. 这些仅适用于iOS 8.0及更高版本。 You get these warnings when your deployment target is set to iOS 7.0 or lower. 当部署目标设置为iOS 7.0或更低版本时,您会收到这些警告。

The way I fixed the warning: 我修正警告的方式:

  • Click the warning in Xcode 单击Xco​​de中的警告
  • Attribute inspector will open the constraint 属性检查器将打开约束
  • Search for item that has margin (see screenshot) 搜索有保证金的项目(见截图)
  • Turn off Relative to margin option 关闭相对于保证金选项

属性检查器

Disable "Prefer margin relative" 禁用“首选保证金相对”

在此输入图像描述

Unchecking the "Prefer Margin Relative" checkbox will keep you from getting into the situation where you get this warning. 取消选中“首选保证金相对”复选框将阻止您进入收到此警告的情况。

If you are like me and you built an entire app with constraints before realizing the problem, then things are a bit tougher because Xcode will not easily tell you which constraints are a problem. 如果你像我一样,你在实现问题之前构建了一个带有约束的整个应用程序,那么事情就更难了,因为Xcode不会轻易告诉你哪些约束是个问题。

In order to avoid rebuilding all of my constraints, I resorted to looking at the actual .storyboard file and I looked for constraints like this: 为了避免重建我的所有约束,我求助于查看实际的.storyboard文件,我寻找这样的约束:

<constraint firstItem="vId-..." firstAttribute="top" secondItem="In7-..." secondAttribute="topMargin" id="C0H-..."/>

Notice "topMargin". 注意“topMargin”。 The attributes ending in "Margin" cause the warning. 以“Margin”结尾的属性会导致警告。 I went through and identified the constraints in the file this way, then I removed and re-created them in IB. 我通过这种方式检查了文件中的约束,然后在IB中删除并重新创建了它们。 After that, this warning went away. 在那之后,这个警告就消失了。

I suspect this should also resolve some inconsistencies between iOS 7 and iOS 8 constraint handling, although I am still encountering some differences in behavior, even after addressing all Xcode warnings. 我怀疑这也应该解决iOS 7和iOS 8约束处理之间的一些不一致,尽管我仍然遇到一些行为上的差异,即使在解决了所有Xcode警告之后。

I'm currently in the same situation as I'm creating an app in Xcode 6 that targets iOS7 and 8. That warning appears because the constraints relative to the margin aren't available in iOS7, but they are created by default in Xcode 6. 我目前处于相同的状态,因为我在Xcode 6中创建了一个针对iOS7和8的应用程序。出现这个警告是因为iOS7中没有相对于边距的约束,但它们是默认情况下在Xcode 6中创建的。

You can absolutely go back and edit the constraint to not use that 'Relative to margin' feature as suggested by the other answers to this question. 您绝对可以返回并编辑约束,以便不使用此问题的其他答案所建议的“相对于保证金”功能。

When creating new constraints, I hold down the Option key when choosing which kind of constraint to create. 在创建新约束时,我在选择要创建的约束类型时按住Option键。 This gives me the ability to create a constraint that's not based on the margin right away so I don't have to go back and fix it afterwards. 这使我能够创建一个不立即基于边距的约束,因此我不必在之后返回并修复它。

Xcode 6 creates constraints based on relative margins by default. 默认情况下,Xcode 6基于相对边距创建约束。

So, if you want to force remove all baselines attributes and don't want to search all bad constrains in Interface Buidler, to support iOS 7 , I can recommend this way 所以,如果你想强制删除所有基线属性并且不想在Interface Buidler中搜索所有不良约束,为了支持iOS 7 ,我可以推荐这种方式

To find and remove all dummy strings with "Baseline" in constraints you can do this: 要在约束中使用“Baseline”查找和删除所有虚拟字符串,您可以执行以下操作:

  1. Close Xcode 关闭Xcode
  2. Open your stroyboard file in your favorite text editor, that supports regexp. 在您喜欢的文本编辑器中打开您的stroyboard文件,该编辑器支持regexp。
  3. Find and remove from storyboard file all strings by pattern: .*"baseline".*\\n 通过模式在storyboard文件中查找并删除所有字符串: .*"baseline".*\\n
  4. Now save file and open it in Xcode 现在保存文件并在Xcode中打开它
  5. Fix all appeared misaligns: find all warnings and press "Update constraints for all views" to save original position of all views. 修复所有出现的错位:找到所有警告并按“更新所有视图的约束”以保存所有视图的原始位置。
  6. Profit! 利润!

UPD: I found that "baseline" constraints causes crashes also, but Xcode doesn't show any warnings about these constrains! UPD:我发现“基线”约束也会导致崩溃,但Xcode没有显示有关这些约束的任何警告!

To fix it - remove from storyboard file all strings by pattern: .*"baseline".*\\n 要解决此问题 - 通过模式从故事板文件中删除所有字符串: .*"baseline".*\\n

I took a non programmer approach. 我采用了非程序员的方法。

I knew which view controler was causing the 8.0 margin message. 我知道哪个视图控制器导致8.0边距消息。 So, I went to my constraint list. 所以,我去了我的约束列表。 I had 33. I've remove every one that was causing the 8.0 margin message. 我有33个。我删除了导致8.0边距消息的每一个。 I went down to 20 (So 11 was problem). 我下降到20(所以11是问题)。

Select 32 over 33 delete and see the result. 选择32 over 33 delete并查看结果。 Select 31 over 33 delete and so on ... Naturally when the message still there ... delete the problematic one. 选择31超过33删除等等......自然当消息仍然存在时...删除有问题的消息。

So it took me 5 minutes to resolve the error message. 所以我花了5分钟来解决错误消息。

Other constraint messages did appear but that's not a big deal. 确实出现了其他约束消息,但这并不是什么大问题。

Ok than, redo and resolve step by step the constraint messages but this time check that all the constraint you add don't provoke the reappearing of the 8.0 margin message. 好了,重做并逐步解决约束消息,但这次检查您添加的所有约束都不会引起8.0边缘消息的重新出现。 If so, undo and take an other strategy when adding constraints. 如果是这样,请在添加约束时撤消并采取其他策略。 There's always an other way to add a constraint that work. 总有一种方法可以添加一个有效的约束。

That's not what is proposed by Xcode but it will do the job. 这不是Xcode提出的,但它会完成这项工作。

At the end, your done . 最后, 你做完了

If you have more than one view controller, and don't know witch one is causing the 8.0 margin message, try to isolate each view controller. 如果您有多个视图控制器,并且不知道是否会导致8.0边距消息,请尝试隔离每个视图控制器。 May be in a separate test project with a copy and paste or deleting all the other view except one and se the result. 可以在一个单独的测试项目中进行复制和粘贴,或者删除除一个以外的所有其他视图,然后查看结果。

Using the "Create Snapshot" in the file menu can help you if something goes wrong. 如果出现问题,使用文件菜单中的“创建快照”可以帮助您。

Suppressing the warnings: I went through all IB constraints and deleted all that said "First Baseline...". 抑制警告:我经历了所有IB约束并删除了所有说“First Baseline ...”。 This made all of these warnings go away. 这使得所有这些警告都消失了。 However, IB will reintroduce the warnings if you "Add missing constraints" or "reset to suggested constraints". 但是,如果您“添加缺失的约束”或“重置为建议的约束”,IB将重新引入警告。

Possible cause: From a fresh Xcode 6.1 project, I copy/pasted ViewControllers in Storyboard (lazily), using "reset to suggested constraints". 可能的原因:从一个新的Xcode 6.1项目,我在Storyboard中复制/粘贴ViewControllers(懒惰),使用“重置为建议的约束”。 Then changed the project target from 8.0 to 7.1. 然后将项目目标从8.0更改为7.1。 This caused the warnings to pop up. 这导致警告弹出。 I noticed that the warnings only occurred on certain of my ViewControllers, which I believe were the ones I copied/pasted in Storyboard. 我注意到警告只发生在我的某些ViewControllers上,我认为这些是我在Storyboard中复制/粘贴的。

Suggestion: If targeting 7.1, set it up front before using IB. 建议:如果以7.1为目标,请在使用IB之前进行设置。 Don't copy/paste view controllers in Storyboard. 不要在Storyboard中复制/粘贴视图控制器。 And be wary of "new warnings" when working within IB, so hopefully we can confirm the cause of this annoyance. 在IB工作时要小心“新警告”,所以我们希望能够确认这种烦恼的原因。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM