iosiphone-softkeyboard

IOS keyboard will not show when add in small piece of code


I have the following custom keyboard 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])


}

For some reason adding in my testOne UIButton causes the keyboard to never showup (instead when I select it the default keyboard just shows up again).

Is my button causing my custom keyboard to crash?

UPDATE ONE: As soon as I uncommented the line testOne.widthAnchor.constraintEqualToAnchor(...).active = true the bug started happening.

Am I not allowed to create custom constraints in the viewDidLoad() method?


Solution

  • Put the line: self.view.addSubview(testOne) before you change any of the constraints.