【问题标题】:AutoLayout complains about constraints for 2 UITextFields with no bordersAutoLayout 抱怨 2 个没有边框的 UITextFields 的约束
【发布时间】:2015-08-30 11:15:48
【问题描述】:

我正在尝试构建只有 2 个文本字段和 2 个按钮的登录视图。我正在应用“添加缺少的约束”属性,以便 Xcode 将相应的约束应用于情节提要中我的 LoginViewController 中的所有元素。我将我的应用程序限制为仅在纵向模式下运行,因此我使用的是“wCompact hRegular”设置。

每当我运行我的应用程序并使用 segue 转换到我的 LoginViewController 时,我的应用程序只显示一个文本字段,并在控制台中显示一堆关于自动布局的错误。

我想要实现的功能是提供一个类似于新 Parse 登录屏幕的登录屏幕。那是有两个没有边框的文本字段,并且只显示用户名文本字段的底部边框,因此它可以将其与密码文本字段分开,如下所示:

到目前为止,这是我的应用运行时的外观:

它应该是这样的:

这是控制台中出现的错误:

2015-06-14 20:40:21.480 MyApp[71158:3600335] Unable to simultaneously satisfy constraints.
    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; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<_UILayoutSupportConstraint:0x7f9ff3f6d260 V:[_UILayoutGuide:0x7f9ff3f7a140(20)]>",
    "<_UILayoutSupportConstraint:0x7f9ff3f087a0 V:|-(0)-[_UILayoutGuide:0x7f9ff3f7a140]   (Names: '|':UIView:0x7f9ff3f7a030 )>",
    "<_UILayoutSupportConstraint:0x7f9ff3f24730 V:[_UILayoutGuide:0x7f9ff3f7a2a0(0)]>",
    "<_UILayoutSupportConstraint:0x7f9ff3f71640 _UILayoutGuide:0x7f9ff3f7a2a0.bottom == UIView:0x7f9ff3f7a030.bottom>",
    "<NSLayoutConstraint:0x7f9ff3f6b9e0 V:[UITextField:0x7f9ff3f782a0]-(NSSpace(8))-[UITextField:0x7f9ff3f7a3d0]>",
    "<NSLayoutConstraint:0x7f9ff3f58a40 V:[UIButton:0x7f9ff3f7ab00'Login']-(331)-[_UILayoutGuide:0x7f9ff3f7a2a0]>",
    "<NSLayoutConstraint:0x7f9ff3f28b60 V:[_UILayoutGuide:0x7f9ff3f7a140]-(237)-[UITextField:0x7f9ff3f782a0]>",
    "<NSLayoutConstraint:0x7f9ff3f34180 V:[UIButton:0x7f9ff3f7ad20'I forgot my password']-(49)-[UIButton:0x7f9ff3f7ab00'Login']>",
    "<NSLayoutConstraint:0x7f9ff3f341d0 V:[UITextField:0x7f9ff3f7a3d0]-(35)-[UIButton:0x7f9ff3f7ad20'I forgot my password']>",
    "<NSLayoutConstraint:0x7f9ff3f29f90 V:[UITextField:0x7f9ff3f782a0(30)]>",
    "<NSLayoutConstraint:0x7f9ff3f055a0 V:[UIButton:0x7f9ff3f7ad20'I forgot my password'(30)]>",
    "<NSLayoutConstraint:0x7f9ff3f055f0 V:[UIButton:0x7f9ff3f7ab00'Login'(30)]>",
    "<NSLayoutConstraint:0x7f9ff3cc3b80 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f9ff3f7a030(667)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f9ff3f055f0 V:[UIButton:0x7f9ff3f7ab00'Login'(30)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

有谁知道如何解决这个问题以及为什么它不起作用?我什至尝试手动手动添加所有约束,但它不起作用。此外,如果我给我的文本字段设置圆角边框并使它们比情节提要的全宽小一点,它就会达到预期的效果。

提前感谢您的帮助。

干杯!

【问题讨论】:

  • 你的约束太多了。如果您为您的文本字段设置顶部、底部和高度约束,那么它们只会满足您的视图在情节提要中的一个特定视图高度。布局的某些部分需要能够更改以适应不同的视图高度。
  • 你的问题解决了吗?
  • 不,我没有。 @dan 你能告诉我一个我怎么做的例子吗?非常感激你的帮助!谢谢!

标签: ios xcode swift autolayout nslayoutconstraint


【解决方案1】:

“添加缺少的约束”并不总是添加约束的好主意。您应该总是更喜欢手动添加约束...

这是您的 UI 的图像...我使用了 wAnyhAny 布局,因为这是为通用设备添加约束的好习惯...

我对文本字段使用了简单的宽度约束,而不是你应该 Equal width 到超级视图并添加乘数以根据设备宽度调整宽度

这里是不同尺寸的输出图像...

【讨论】:

  • 虽然这不是我问题的直接答案,但您向我展示了一些很酷的技巧,多亏了这些技巧,我设法做到了我想要的...谢谢您的帮助!跨度>
猜你喜欢
  • 2017-09-03
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2018-05-18
相关资源
最近更新 更多