简体   繁体   English

视图的自动布局可隐藏子视图

[英]Auto Layout of view hides subviews

I have a UIViewController embedded in a Navigation Controller. 我在导航控制器中嵌入了UIViewController。 I am using autolayout. 我正在使用自动版式。 In interface builder this is how the VC looks. 在界面生成器中,这就是VC的外观。

故事板

But when run in the simulator I get this: 但是当在模拟器中运行时,我得到以下信息:

With this error. 出现这个错误。

2016-08-10 16:51:17.077 My App[11797:7752671] Unable to simultaneously satisfy constraints. 2016-08-10 16:51:17.077我的应用程序[11797:7752671]无法同时满足约束。 Probably at least one of the constraints in the following list is one you don't want. 以下列表中至少有一个约束是您不想要的约束。 Try this: (1) look at each constraint and try to figure out which you don't expect; 尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束; (2) find the code that added the unwanted constraint or constraints and fix it. (2)查找添加了一个或多个不必要约束的代码并进行修复。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<_UILayoutSupportConstraint:0x7fad72f7d6b0 V:[_UILayoutGuide:0x7fad72e5d1d0(64)]>", "<_UILayoutSupportConstraint:0x7fad72ffa6a0 V:|-(0)-[_UILayoutGuide:0x7fad72e5d1d0] (Names: '|':UIView:0x7fad72fe1b40 )>", "<_UILayoutSupportConstraint:0x7fad72fe2460 V:[_UILayoutGuide:0x7fad72e64760(0)]>", "<_UILayoutSupportConstraint:0x7fad72fe1470 _UILayoutGuide:0x7fad72e64760.bottom == UIView:0x7fad72fe1b40.bottom>", "", "", "", "") (注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性的文档translationsAutoresizingMaskIntoConstraints)(“ <_UILayoutSupportConstraint:0x7fad72f7d6b0 V:[_ UILayoutGuide:0x7fad72e5d1d0(stra)<64]> V:|-(0)-[_ UILayoutGuide:0x7fad72e5d1d0](名称:'|':UIView:0x7fad72fe1b40)>“,” <_UILayoutSupportConstraint:0x7fad72fe2460 V:[_ UILayoutGuide:0x7fad72e47060(0)]>“,” <__ _UILayoutGuide:0x7fad72e64760.bottom == UIView:0x7fad72fe1b40.bottom>“,”“,”“,”“,”“)

Will attempt to recover by breaking constraint 将尝试通过打破约束来恢复

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

I have rebuilt this view several times. 我已经多次重建了这种观点。 I go up to Editor > Resolve Auto Layout Issues > "All Views" Reset to Suggested Constrains . 我转到“ 编辑器” >“ 解决自动布局问题” > “所有视图”重置为“建议约束”

Which generates this: 哪个生成此:

情节提要中的约束

I have even manually deleted all constrains and started over. 我什至手动删除了所有约束并重新开始。

But nothing works. 但是什么都行不通。

I have nothing in the .m file that lays anything out or adjust sizes or locations. .m文件中没有任何内容可以布局或调整大小或位置。 Nothing fancy is happening with this view. 这种观点毫无幻想。 What am I doing wrong? 我究竟做错了什么?

Editor > Resolve Auto Layout Issues > "All Views" Reset to Suggested Constrains almost never works correctly. 编辑器>解决自动布局问题>“所有视图”重置为建议的约束几乎永远无法正常工作。 So just avoid it. 所以就避免吧。

When adding constraints, you need to specify four things for every item: 添加约束时,需要为每个项目指定四件事:

  1. X location X位置
  2. Y location Y位置
  3. Width 宽度
  4. Height 高度

There are creative ways to do this, so for you I would suggest you add these constraints to the Lookup Button: 有一些创造性的方法可以做到这一点,因此建议您向“查找”按钮添加以下约束:

  • Horizontally in center 水平居中
  • Leading Space To Superview or Trailing Space to Superview (you do not need both) 前导空间到Superview 尾随空间到Superview(您无需同时使用两者)
  • Height 高度
  • Top Space to Call Number 呼叫号码的顶部空间

Your x location and width are satisfied by Horizontally in Center and Leading or Trailing Space. 您的x位置和宽度由“居中水平”和“前导或尾随空间”满足。 These two constraints center the button and give you equal space on either side of the button. 这两个约束使按钮居中,并在按钮的两侧留出相等的空间。 The button will be as wide as the view minus the space on both sides. 该按钮的宽度将等于视图减去两侧的空间。 Setting the height obviously satisfies the height of the button and Top Space satisfies the y position of your button. 设置高度显然可以满足按钮的高度,并且“顶部空间”可以满足按钮的y位置。 Therefore you have easily satisfied x location, y location, width and height of the button, and you are done. 因此,您很容易满足按钮的x位置,y位置,宽度和高度,就可以完成。

plz set your constraints as following image 请设置您的约束如下图

在此处输入图片说明 在此处输入图片说明

You need to start over and think about constraints you are making. 您需要重新开始,并考虑您正在制定的约束。 Think about what information is known, what sizes should not change. 考虑一下已知的信息,什么大小不应更改。 Tell this information to Auto Layout. 将此信息告知自动版式。

Your button is not appearing because Auto Layout does not known how to position it, it missing following numbers. 您的按钮未出现,因为“自动版面配置”不知道如何放置,因为缺少以下数字。 Delete all constraints and start over with the following. 删除所有约束并重新开始以下操作。 Not you can change this numbers. 不是,您可以更改此数字。

  1. Txt Search.top = Top Layout Guide.bottom. Txt Search.top =顶部布局指南。底部。 This is yours, it is correct. 这是你的,是正确的。
  2. Txt Search.height = 20.0 Txt Search.height = 20.0
  3. Txt Search.leading = 0 with margin Txt Search。Lead = 0有边距
  4. Txt Search.trailing = 0 with margin Txt Search.trailing = 0有边距
  5. Lookup.top = Txt.Search.bottom + standard margin Lookup.top = Txt.Search.bottom +标准边距
  6. Lookup.leading = 0 with margin Lookup。Lead = 0有保证金
  7. Lookup.trailing = 0 with margin Lookup.trailing = 0有边距
  8. Lookup.height = 20.0 Lookup.height = 20.0

Reset to suggested constraints never solves your problem, it only hides the warning. 重置为建议的约束永远不会解决您的问题,只会隐藏警告。 You have to set the constraints that suits for your requirement. 您必须设置适合您要求的约束。 From your question, when you choose the option, Reset to suggested constraints, the autolayout will give a bottom layout constraint for the LOOK UP button,and it had too much of bottom constraint(which happens when you rest to suggested constraint), hence it is pushed up in the runtime. 根据您的问题,当您选择“重置为建议的约束”选项时,自动布局将为“向上查看”按钮提供底部布局约束,并且其底部约束过多(当您休息到建议的约束时会发生这种情况),因此在运行时被上推。

You just need top and leading/ top and center horizontal constraint for your button corresponding to its above textfield along with height and width constraint. 您只需要按钮的顶部和顶部/顶部和中央水平约束(对应于其上方的文本字段)以及高度和宽度约束即可。 Remove the bototm constraint for your LOOK up button. 删除您的LOOK向上按钮的bototm约束。

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

相关问题 (自动布局)容器视图调整大小但子视图不调整大小 - (auto layout) container view resizes but subviews don't 使用Masonry将自动布局约束添加到视图的子视图 - Adding auto-layout constraints to subviews of a view with Masonry 使用自动布局时,在init上的子视图中查看控制器动画? - View controller animating in subviews on init when using auto layout? 如何在iOS 6中使用具有多个子视图的现有自定义视图自动布局 - How to Auto Layout with existing Custom View with multiple subviews in iOS 6 iOS自动布局,如何调整超级视图的大小以适合所有子视图 - iOS auto layout, how to resize super view to fit all subviews 使用自动布局调整同级视图的大小时,无法重新定位带有子视图的UIView - Reposition of UIView with subviews not working when resizing a sibling view with auto layout 删除视图(和子视图)的自动布局约束,但保持相同的帧位置 - removing auto layout constraints for a view (and subviews) but keep the same frame positions 防止视图布局子视图 - Prevent view to layout Subviews 什么时候布局UIViewControllers视图的子视图? - When to layout subviews of UIViewControllers view? 如何使用自动布局根据其子视图调整父视图的高度? - How to adjust the parent view's height according to its subviews using auto layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM