IOS 添加一小段代码时键盘不会显示
IOS keyboard will not show when add in small piece of code
我有以下自定义键盘代码:
override func viewDidLoad() {
super.viewDidLoad()
//As soon as I add in this code the keyboard will not showup and instead
//selecting this keyboard just reverts back to the normal keyboard
let testOne = UIButton()
testOne.backgroundColor = UIColor.blackColor()
testOne.translatesAutoresizingMaskIntoConstraints = false
testOne.widthAnchor.constraintEqualToAnchor(self.view.widthAnchor, multiplier: 0.5).active = true
testOne.heightAnchor.constraintEqualToAnchor(self.view.heightAnchor, multiplier: 0.5).active = true
testOne.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
testOne.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
self.view.addSubview(testOne)
// Default system generated code that creates a "next keyboard" button.
self.nextKeyboardButton = UIButton(type: .System)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
}
出于某种原因,添加我的 testOne
UIButton
会导致键盘永远不会显示(相反,当我 select 它会再次显示默认键盘)。
我的按钮是否导致我的自定义键盘崩溃?
更新一:一旦我取消注释行 testOne.widthAnchor.constraintEqualToAnchor(...).active = true
错误开始发生。
是否不允许我在 viewDidLoad()
方法中创建自定义约束?
放行:self.view.addSubview(testOne)
在更改任何约束之前。
我有以下自定义键盘代码:
override func viewDidLoad() {
super.viewDidLoad()
//As soon as I add in this code the keyboard will not showup and instead
//selecting this keyboard just reverts back to the normal keyboard
let testOne = UIButton()
testOne.backgroundColor = UIColor.blackColor()
testOne.translatesAutoresizingMaskIntoConstraints = false
testOne.widthAnchor.constraintEqualToAnchor(self.view.widthAnchor, multiplier: 0.5).active = true
testOne.heightAnchor.constraintEqualToAnchor(self.view.heightAnchor, multiplier: 0.5).active = true
testOne.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor).active = true
testOne.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
self.view.addSubview(testOne)
// Default system generated code that creates a "next keyboard" button.
self.nextKeyboardButton = UIButton(type: .System)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
}
出于某种原因,添加我的 testOne
UIButton
会导致键盘永远不会显示(相反,当我 select 它会再次显示默认键盘)。
我的按钮是否导致我的自定义键盘崩溃?
更新一:一旦我取消注释行 testOne.widthAnchor.constraintEqualToAnchor(...).active = true
错误开始发生。
是否不允许我在 viewDidLoad()
方法中创建自定义约束?
放行:self.view.addSubview(testOne)
在更改任何约束之前。