How to detect Orientation Change in Custom Keyboard Extension in iOS 8?

后端 未结 10 1915

In Custom Keyboard Extension , we can\'t use

`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation` 

and

10条回答
  •  醉酒成梦
    2020-12-04 18:22

    for those who are searching for the answer in Swift 5.

    by override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) method. you can detect device orientation.

    here is the code for my custom keyboard.

    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
            let screen = UIScreen.main.bounds
            if screen.width < screen.height {
                print("!!! portrait")
                let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 325)
                constraintForHeight.isActive = true
                constraintForHeight.priority = UILayoutPriority.defaultHigh
                self.inputView?.addConstraint(constraintForHeight)
            } else {
                print("!!! landspace")
                let constraintForHeight:NSLayoutConstraint = NSLayoutConstraint(item: mainView!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 210)
                constraintForHeight.isActive = true
                constraintForHeight.priority = UILayoutPriority.defaultHigh
                self.inputView?.addConstraint(constraintForHeight)
            }
        }
    

提交回复
热议问题