
我试图将一个按钮固定在四个面上,以将一个按钮对象固定在我任意放置它的确切位置,但当我运行它时,我得到了以下错误。任何见解或帮助都将不胜感激。
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:0x9f494b0 V:[_UILayoutGuide:0x9f40370(20)]>",
"<_UILayoutSupportConstraint:0x9f3b6c0 V:|-(0)-[_UILayoutGuide:0x9f40370] (Names: '|':UIView:0x9f3feb0 )>",
"<_UILayoutSupportConstraint:0x9f418e0 V:[_UILayoutGuide:0x9f407c0(0)]>",
"<_UILayoutSupportConstraint:0x9f18010 _UILayoutGuide:0x9f407c0.bottom == UIView:0x9f3feb0.bottom>",
"<NSLayoutConstraint:0x9f40f10 V:[UIButton:0x9f40f40'Button']-(211)-[_UILayoutGuide:0x9f407c0]>",
"<NSLayoutConstraint:0x9f47ab0 V:[_UILayoutGuide:0x9f40370]-(539)-[UIButton:0x9f40f40'Button']>",
"<NSLayoutConstraint:0xa157b10 'UIView-Encapsulated-Layout-Height' V:[UIView:0x9f3feb0(568)]>"
)
Will attempt to recover by breaking constraint
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.发布于 2015-08-08 01:55:56
你会得到这个错误,因为你有相互冲突的约束。您可能想要删除Interface Builder中的一个约束来解决它。
我怀疑你可能是iOS的新手,所以我建议你看看界面构建器的教程,而不是冗长冗长的答案。Here's one from Ray Wenderlich您可能会发现有帮助:
更新:根据你的屏幕截图,我敢打赌你的按钮的大小是导致问题的原因。我认为固定在右侧和底部可以平息控制台中记录的错误。在场景中没有其他项目的情况下,我认为您不需要顶部和左侧约束。
更新2:
另一种方法是使用>=或<=而不是硬编码的=来设置约束。如果将顶部设置为>=,则底部约束将为<=。如果左侧约束为>=,则右侧约束为<=。
更新3:转到iTunesU,观看Stanford University's course titled CS193P, Winter 2015的演示,Lection1接近尾声。他在那次演讲中介绍了基本的自动布局。然后观看课程8,该课程广泛地介绍了自动布局。他在那里解释得非常透彻。
对于Autolayout来说,你是在Swift还是Objective-C中工作并不重要。看到它的实际操作应该会让你很清楚。
发布于 2015-08-08 03:35:49
即使@AdrianB在他的回答中对我的评论也有一个基本的理解,我会在这里详细解释一下:
假设视图的大小为200x200,按钮的大小为100x20。(值仅用于示例)。所以,也就是说,你把它放在左边的空格80,右边的空格20 (80+100+20=200),顶部的空格100,底部的空格80。(100+20+80=200)。
现在视图的大小发生了变化。它的宽度为300。您对右和左的约束仍然有效。因此边框仍然是80和20。实现这一点的唯一方法是将按钮的宽度设置为200。(80+200+20=300)。好吧,也许是个看起来很滑稽的按钮,但这行得通。
但是,如果视图的高度更改为300,那么在垂直方向上就会变得很困难。按钮必须在垂直方向上增长到120。这是顶部和底部空间保持有效的唯一方法。我不认为那样的按钮。
然而,当视图的高度小于180时,这就变得不可能了,因为所有的空间都被您的约束(top=100+bottom=80=180)占用了,所以没有更多的空间来放置底部。它的高度变为负数。
这就是问题所在。
现在来看解决方案:
不要在自动版面中放置对所有四个边都有硬约束的按钮。自动布局用于更改超级视图的大小。在这种情况下,你所做的是没有意义的。
发布于 2015-08-08 16:29:33
你不能有一个顶部有539个点,底部有211个点的按钮,总共750个点,不包括按钮高度。没有屏幕那么大的iPhone屏幕,iPhone 6+的高度是736磅,如果你要硬编码值,你应该调整按钮的539,211和高度,并确保它们加起来等于你要调整大小的iPhone的屏幕高度。
这是一个等于100点的宽度约束:
[self addConstraint:[NSLayoutConstraint constraintWithItem:_avatarImage
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.f
constant:100.f]];这是一个等于100点的宽度约束:
[self addConstraint:[NSLayoutConstraint constraintWithItem:_avatarImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.f
constant:100.f]];您还可以设置其他类型的约束,如中心x:
[self addConstraint:[NSLayoutConstraint constraintWithItem:_avatarImage
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.f
constant:0.f]];avatarImage是一个100x100的图像视图,以超级视图中心x为中心,y的中心没有显示,但我也有一个约束。
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
#define MAX_SCREEN_HEIGHT_IS_IPHONE6_PLUS 736.00
#define MAX_SCREEN_WIDTH_IS_IPHONE6_PLUS 414.00
#define heightResizer(n) n/MAX_SCREEN_HEIGHT_IS_IPHONE6_PLUS*SCREEN_HEIGHT
#define widthResizer(n) n/MAX_SCREEN_WIDTH_IS_IPHONE6_PLUS*SCREEN_WIDTH所以,举个例子,看看这个约束:
[self addConstraint:[NSLayoutConstraint constraintWithItem:_avatarImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.f
constant:100.f]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:_avatarImage
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.f
constant:heightResizer(100.0)]];现在当iPhone大小小于iPhone 6+时,hieght会自动调整大小,这就是这个宏的美妙之处,你可以硬编码一些数字,而另一些你可以让自动布局引擎伸缩,这两种方法都伸缩,而且都很容易使用。导航栏高度和选项卡栏控制器高度会有一些问题,但是如果你知道如何子类化这些UI元素,那么它们就不再是问题了。我子类化所有的东西,我甚至子类化我自己的子类,所以它使用这种混合布局的形式使所有的东西都能很好地工作。
https://stackoverflow.com/questions/31883513
复制相似问题