【发布时间】:2014-10-27 03:03:34
【问题描述】:
刚开始学习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)]];
它引发了一个异常,
无法同时满足约束。
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;
我想知道有什么问题吗?非常感谢您的帮助:)
仅供参考:我在 iOS8 SDK 上工作
【问题讨论】:
-
似乎是一组完全合乎逻辑的约束。在过去的几天里,我一直在为类似的事情头疼。顺便说一句,@Chamira Fernando 按钮上还有其他宽度限制吗?内在内容大小等?您是否尝试将compressionResistancePriority 或contentHuggingPriority 设置为高值或低值?看看会发生什么?
-
约束失败的原因很明显是
UIView-Encapsulated-Layout-Width的宽度为0,UIInputView在哪里? -
真傻 - 我没看到 (0)。我遇到过同样的问题。它是由 UIInputViewController 提供的 UIInputView - 键盘扩展 (iOS8) 的根视图。我不知道为什么它需要 0 的宽度,考虑到默认行为(根据文档)应该坚持屏幕的宽度。
-
@DevKanchen 就在这里,UIInputView 为零是问题,从技术上讲,默认情况下它至少应该获得键盘可用空间的大小。但事实并非如此。所以我必须根据设备类型和 viewDidLoad 方法中的初始 UIInputView 来获取默认键盘大小。
标签: ios objective-c autolayout