2014-09-02 57 views
29

刚刚开始学习iOS的自动版式,界面生成非常简单的,但是当我试图存档同一件事上的代码自动版式,无法同时满足的约束

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(==2)-[_nextKeyboardButton]-(==2)-[_numPadButton]-(==2)-[_spaceButton]-(==2)-[_returnButton]-(==2)-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_nextKeyboardButton,_numPadButton,_spaceButton,_returnButton)]]; 

它抛出一个异常,

无法同时满足约束条件。

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) 
(
"<NSLayoutConstraint:0x6000000966c0 H:|-(2)-[UIButton:0x7fe4f1d1c760'Next'] (Names: '|':UIInputView:0x7fe4f1f04d00)>", 
"<NSLayoutConstraint:0x600000096710 H:[UIButton:0x7fe4f1d1c760'Next']-(2)-[UIButton:0x7fe4f1d1d1d0'123']>", 
"<NSLayoutConstraint:0x600000096760 H:[UIButton:0x7fe4f1d1d1d0'123']-(2)-[UIButton:0x7fe4f1d1d6f0'Space']>", 
"<NSLayoutConstraint:0x6000000967b0 H:[UIButton:0x7fe4f1d1d6f0'Space']-(2)-[UIButton:0x7fe4f1d1d8d0'Return']>", 
"<NSLayoutConstraint:0x600000096800 H:[UIButton:0x7fe4f1d1d8d0'Return']-(2)-| (Names: '|':UIInputView:0x7fe4f1f04d00)>", 
"<NSLayoutConstraint:0x600000096e40 'UIView-Encapsulated-Layout-Width' H:[UIInputView:0x7fe4f1f04d00(0)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000967b0 H:[UIButton:0x7fe4f1d1d6f0'Space']-(2)-[UIButton:0x7fe4f1d1d8d0'Return']> 

所有4个按钮.translatesAutoresizingMaskIntoConstraints = NO;

我想知道什么是错的?帮助是非常感谢:)

FYI:我在iOS8上的SDK

+1

似乎是一个完全合乎逻辑的约束条件。在过去的几天里,我一直在抨击我的头脑。顺便说一句,@Chamira Fernando在按钮上还有其他宽度限制吗?内部内容大小等?您是否尝试将compressionResistancePriority或contentHuggingPriority设置为高或低值?只要看看会发生什么? – 2014-09-18 11:35:50

+3

约束失败的原因显然是由'UIView-Encapsulated-Layout-Width'的宽度为0.'UIInputView'位于何处? – 2014-09-18 12:17:24

+0

傻我 - 我没有看到(0)。我遇到过同样的问题。它是由UIInputViewController提供的UIInputView - 键盘扩展的根视图(iOS8)。我不知道为什么它会希望宽度为0,考虑到默认行为(根据文档)应该坚持屏幕宽度。 – 2014-09-18 22:05:42

回答

2

工作,根据你的问题就我的理解,你已经创建了笔尖控制和您直接试图改变是从你的类的限制。

你可以在这里做的是在视图控制器的nib中设置约束,然后将它与你的类绑定并使用约束对象。或者你可以编程创建视图并直接设置约束。

//如果“是 - 视图的超级视图查看视图的自动调整屏蔽,产生实现它的约束并将这些约束添加到自身(超级视图),您还需要检查是否需要”translatesAutoresizingMaskIntoConstraints“。 ”如果‘No - 视图的上海华盈不看视图的自动尺寸面具,而不是产生了实现它的约束’

我也得到了同样的问题,我修好了如下

 [_nextKeyboardButton setTranslatesAutoresizingMaskIntoConstraints:YES]; 
    _numPadButton.translatesAutoresizingMaskIntoConstraints = YES; 
    [_numPadButton updateConstraints]; 
    [_nextKeyboardButton updateConstraints]; 

    NSArray *constraints2 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-57-[_nextKeyboardButton(96)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_nextKeyboardButton)]; 

    [self.view addConstraints:constraints2]; 

    NSArray *constraints4 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-123-[_nextKeyboardButton(30)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_nextKeyboardButton)]; 

    [self.view addConstraints:constraints4]; 

    NSArray *constraints1 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-207-[_numPadButton(58)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_numPadButton)]; 

    [self.view addConstraints:constraints1]; 

    NSArray *constraints3 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-123-[_numPadButton(30)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_numPadButton)]; 

    [self.view addConstraints:constraints3]; 

// Create Controls or view programmatically. 

UILabel *label = [UILabel new]; 
    label.text = @"This is a Label"; 
    label.font = [UIFont systemFontOfSize:18]; 
    label.backgroundColor = [UIColor lightGrayColor]; 
    label.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:label]; 

    NSArray *constraints = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[label(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(label)]; 
    [self.view addConstraints:constraints]; 

    UIView *spacer1 = [UIView new]; 
    spacer1.translatesAutoresizingMaskIntoConstraints = NO; 
    [spacer1 setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:spacer1]; 

    UIView *spacer2 = [UIView new]; 
    spacer2.translatesAutoresizingMaskIntoConstraints = NO; 
    [spacer2 setBackgroundColor:[UIColor greenColor]]; 
    [self.view addSubview:spacer2]; 

    NSArray *constraints1 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[spacer1(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(spacer1)]; 

    [self.view addConstraints:constraints1]; 

    NSArray *constraints2 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[spacer2(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(spacer2)]; 

    [self.view addConstraints:constraints2]; 

    [self.view addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"H:|[spacer1]-10-[label]-20-[spacer2(==spacer1)]|" 
           options:0 
           metrics:nil 
           views:NSDictionaryOfVariableBindings(label, spacer1, spacer2)]]; 

所以当使用AutoLayout时,你应该从不直接设置视图的框架。约束被用来为你做这件事。通常,如果你想在视图中设置你自己的约束,你应该重写你的UIViews的updateConstraints方法。确保页面控制器的内容视图允许调整其边缘的大小,因为它们的大小将适合页面视图的框架。你的约束和视图设置将需要考虑到这一点,否则你将得到不可满足的约束错误。

https://developer.apple.com/library/mac/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutConcepts/AutoLayoutConcepts.html#//apple_ref/doc/uid/TP40010853-CH14-SW1 你也可以参考上面的苹果链接深入研究,如果你仍然面临问题,我会尽我所能的水平来解决你的问题。

8

当我尝试在基于故事板的视图控制器中手动创建所有自动布局约束时(在Swift中,使用Snappy - Swift的Masonry端口),我不得不努力解决类似问题。

由于某些原因,Xcode在构建时会在NIB上生成自己的默认自动布局约束集。这就是为什么我不能添加任何手动约束,因为它们与自动添加的约束相冲突。

我解决了这个方式如下:

  • 打开你处理故事板视图控制器。

  • 选择视图控制器和选择编辑>解决自动布局问题> []中视图控制器的所有视图>从菜单中选择添加缺少约束

enter image description here

(这将确保不会产生额外的构建时间限制,并且所有约束现在都可见)。

  • 从您的视图控制器选择所有的约束条件:
从右侧窗格中的下列复选框

enter image description here

  • 检查:占位符 - 在构建时删除:

enter image description here

现在您可以在代码中手动添加所有自动布局约束。

+1

这对我来说完全排除了问题。 – aframe 2015-01-26 11:12:53

+0

我遇到了类似的问题,我有七个UIButtons排成一行。在StoryBoard中一切都很好,但是当我构建它时,我得到了很多“无法同时满足约束”的例外,但它在模拟器上看起来很好。我想知道我是否应该对他们做些什么?如果我这样做,我不能使用编辑器解决约束问题,因为我的StoryBoard中没有任何内容。 – Liumx31 2015-09-01 03:07:03

77

如何找到不可满足的约束条件最简单的方法:

  • 设置的唯一标识您的视图中的每个约束:

enter image description here

  • NSLayoutConstraint创建简单的扩展:

SWIFT

extension NSLayoutConstraint { 

    override public var description: String { 
     let id = identifier ?? "" 
     return "id: \(id), constant: \(constant)" //you may print whatever you want here 
    } 
} 

Objective-C的

@interface NSLayoutConstraint (Description) 

@end 

@implementation NSLayoutConstraint (Description) 

-(NSString *)description { 
    return [NSString stringWithFormat:@"id: %@, constant: %f", self.identifier, self.constant]; 
} 

@end 
  • 构建它再次,现在你有更多的可读性为你输出:

enter image description here

  • ,一旦你有你的id你可以简单的点击它你查找导航

enter image description here

  • ,并迅速找到它:

enter image description here

如何简单地修复这种情况?

  • 尝试改变优先999断开的约束。

看到相关答案在这里:Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint

+9

一个令人难以置信的提示。 – Fattie 2015-09-23 12:17:34

+1

这是我读过的用于调试约束问题的最有用的东西。把手放下。谢谢! – Xeaza 2015-12-03 02:01:16

+0

ty,这项工作不错 – 2015-12-18 20:59:37

3

这可能是不那么difficult.The消息控制台输出表示,UIButton的的限制redefined.It意味着只是喜欢你把限制Height和约束Width这个按钮,但是不必要的是你再加上一个限制,即按钮有一个Aspect Ratio。在这种情况下,Xcode不能确定遵循哪个约束,以便你可以在控制台中看到这个调试消息。

有两种方法有用的我:

  1. 分析显示在控制台的限制,并尝试删除一个或多个约束。

2。如果您使用的是storyboardXib,请选择约束 - >显示属性检查器 - >更改某些约束的优先级。


一件事...

您可以按照杰森贾勒特的article。添加一个符号断点以确保哪个视图使错误发生。

1

万一别人运行到这一点,一个简单的方法来直观地检查你的约束问题,是这个网站(它真的有用!):

http://www.wtfautolayout.com

下面是情况下,Github上项目现场下降: https://github.com/johnpatrickmorgan/wtfautolayout

+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效 – slfan 2017-09-04 20:45:07

相关问题