AutoLayout,无法同时满足约束 [英] AutoLayout, Unable to simultaneously satisfy constraints

查看:148
本文介绍了AutoLayout,无法同时满足约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚开始学习iOS AutoLayout,接口构建器非常直接,但是当我尝试在代码上存档相同的东西时

  [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)]]; 

它会引发异常,



无法同时满足约束条件。

 以下列表中的至少一个约束可能是你的一个不想要。试试这个:(1)看看每个约束并试着找出你不期望的东西; (2)找到添加了不需要的约束或约束的代码并修复它。 (注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)

< NSLayoutConstraint:0x6000000966c0 H:| - (2) - [ UIButton:0x7fe4f1d1c760'Next'](名称:'|':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'返回'] - (2) - |(名称:'|':UIInputView:0x7fe4f1f04d00)>,
< NSLayoutConstraint:0x600000096e40'UIView-Encapsulated-Layout-Width'H:[UIInputView:0x7fe4f1f04d00( 0)]>


将尝试通过违约约束来恢复
< NSLayoutConstraint:0x6000000967b0 H:[UIButton:0x7fe4f1d1d6f0'Space'] - (2) - [UIButton:0x7fe4f1d1d8d0'Return']>

所有4个按钮.translatesAutoresizingMaskIntoConstraints = NO;



<我想知道出了什么问题?非常感谢帮助:)



仅供参考:我在iOS8 SDK上工作

解决方案

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




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






  • NSLayoutConstraint 创建简单扩展名:



SWIFT

  extension NSLayoutConstraint {

覆盖public var description:String {
let id =标识符?
返回id:\(id),常数:\(常数)//你可以在这里打印任何你想要的东西
}
}

目标-C

  @interface NSLayoutConstraint(Description)

@end

@implementation NSLayoutConstraint(Description)

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

@end




  • 再次构建它,现在你有更多可读输出:






  • 一旦你拿到 id ,你可以在你的中轻点点击它查找导航器






  • 并快速找到它:





如何简单修复案例?




  • 尝试将优先级更改为 999 用于破坏约束。



请参阅此处的相关答案:无法同时满足约束条件,将尝试通过违反约束来恢复


Just started learning iOS AutoLayout, Interface builder very straight forward, but when I try to archive the same thing on the code

    [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)]];

it raises an exception,

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)
(
 "<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']>

All 4 buttons .translatesAutoresizingMaskIntoConstraints = NO;

I wonder what is wrong ? help is really appreciated :)

FYI: I work on iOS8 SDK

解决方案

The easiest way how to find unsatisfiable constraints:

  • set unique identifier for every constraint in your view:

  • create simple extension for 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

  • build it once again, and now you have more readable output for you:

  • once you got your id you can simple tap it in your Find Navigator:

  • and quickly find it:

HOW TO SIMPLE FIX THAT CASE?

  • try to change priority to 999 for broken constraint.

See the related answer here: Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint

这篇关于AutoLayout,无法同时满足约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆