AutoLayout抱怨没有边框的2个UITextFields的约束

时间:2015-06-15 03:45:20

标签: ios xcode swift autolayout nslayoutconstraint

我正在尝试构建只有2个TextField和2个按钮的登录视图。我正在应用"添加缺失约束"属性,以便Xcode将相应的约束应用于故事板中我的LoginViewController中的所有元素。我限制我的应用程序只在纵向模式下运行,所以我使用" wCompact hRegular"设置。

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

我想要实现的功能是提供类似于新Parse登录屏幕的登录屏幕。这是两个没有边框的文本字段,只有用户名文本字段的下边框显示,因此可以将其从密码文本字段中分割出来,如下所示:

enter image description here

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

enter image description here

看起来应该是这样的:

enter image description here

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

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.

有谁知道如何解决这个问题以及为什么它不起作用?我甚至尝试手动添加所有约束,但它不起作用。此外,如果我将文本字段设置为圆形边框并使它们比故事板的全宽度小一些,那么它就会完成预期的操作。

提前感谢您的帮助。

干杯!

1 个答案:

答案 0 :(得分:4)

&#34;添加缺失约束&#34;添加约束并不总是一个好主意..你应该总是喜欢手动添加约束......

以下是您的用户界面的图片...我使用wAnyhAny布局,因为为通用设备添加约束是一种很好的做法......

enter image description here

  

我只使用文本字段的宽度约束,而应该Equal width超级查看并根据设备宽度添加乘数以调整宽度

以下是不同尺寸的输出图像...

enter image description here