Swift - 自定义键盘高度

时间:2014-09-17 10:26:34

标签: iphone swift ios8

我创建了一个带有xib文件的自定义键盘。它可以像我想的那样完美地工作,但是在调试键盘时我收到很多警告。我在xib中创建了所有约束并将其作为子视图添加到我的视图中。

var nib = UINib(nibName: "Keyboard", bundle: nil)
let objects = nib.instantiateWithOwner(self, options: nil)
mainView = objects.first as UIView
view.addSubview(mainView)

我在viewDidLoad() - 方法中执行此操作。在viewDidAppear()中,我调用它来获取我的身高:

let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 180.0)
        view.addConstraint(heightConstraint)

mainView.frame = view.bounds

但现在我收到了这个警告:

2014-09-17 12:21:21.689 Hodor[6629:1086407] 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:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>",
    "<NSLayoutConstraint:0x1780882f0 V:[UIInputView:0x15e607830(180)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>

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.
2014-09-17 12:21:21.693 Hodor[6629:1086407] 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:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>",
    "<NSLayoutConstraint:0x1780882f0 V:[UIInputView:0x15e607830(180)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>

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.
2014-09-17 12:21:21.697 Hodor[6629:1086407] 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:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>",
    "<NSLayoutConstraint:0x1780882f0 V:[UIInputView:0x15e607830(180)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x178087e40 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x15e607830(264)]>

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.

这是什么意思?从警告的文本​​看,它似乎是高度约束的东西......

2 个答案:

答案 0 :(得分:2)

试试这个

view.setTranslatesAutoresizingMaskIntoConstraints(假) view.addConstraint(NSLayoutConstraint(             item:mainView,属性:.Left,             relatedBy:.Equal,toItem:view,             attribute:NSLayoutAttribute.Left,multiplier:0.6,constant:0))

    view.addConstraint(NSLayoutConstraint(
        item:mainView, attribute:.Right,
        relatedBy:.Equal, toItem:view,
        attribute:.RightMargin,multiplier:0.6, constant: 0))

    view.addConstraint(NSLayoutConstraint(
        item:mainView, attribute:.Width,
        relatedBy:.Equal, toItem:view,
        attribute: .Width,multiplier:0.6, constant: 0))

    view.addConstraint(NSLayoutConstraint(
        item:mainView, attribute:.Height,
        relatedBy:.Equal, toItem:view,
        attribute:.Height,multiplier:0.6, constant: 0))

答案 1 :(得分:1)

存在一些与约束的冲突。自动调整大小与您应用的约束相冲突。

相关问题