Keyboard extension loses height in iOS 10 when trying to size automatically in some cases

梦想与她 提交于 2019-12-03 07:51:29

Apple has responded to my DTS ticket and told me to file a bug report, so this is actually an iOS 10 bug. I have filed a radar (#28532959) and will update this answer if I ever get a response. If someone else comes up with a concrete solution that allows me to still use autolayout to achieve an automatic height, answers are still accepted.

Here's my workaround. It is a little laggy when the device rotates, but it will do the job until Apple fixes this bug. I first thought it had something to do with inputView.allowSelfSizing, but that variable didn't seem to change anything.

First, declare heightConstraint:

var heightConstraint: NSLayoutConstraint!

In viewDidLoad, Add your custom view:

let nibName: String! = UIDevice.isPhone ? "KeyboardViewiPhone" : "KeyboardViewiPad"
customView = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! UIView
customView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(customView)

Add a constraint for the width as you would do normally:

let widthConstraint = NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: customView, attribute: .width, multiplier: 1.0, constant: 0.0)

Add a constant constraint for the height:

heightConstraint = NSLayoutConstraint(item: customView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: view.frame.height)
view.addConstraints([widthConstraint, heightConstraint])

Now comes the fix:

override func viewDidLayoutSubviews() {
    heightConstraint.constant = view.bounds.height
}

As viewDidLayoutSubviews is called every time view.bounds changes, it will handle orientation changes correctly.

I also faced the same issue. this is because of the Autolayout Constraints. Just remove all constraints. and set auto resizing.

I got it solved by setting a new constrain for the height.

I have also faced the same problem for the custom keyboard extension in Xcode 8.2. This is caused by the auto resizing. In my case, I solved this in the below manner.

Initially, my custom keyboard have 3 views. In this, I was fixed the trailing, leading, top and height for the first and last view. And place the middle view like in the image.

after that select the middle view and open the show the size inspector in the storyboard. In the size inspector, you will find an option auto resizing. In that select the constraint indicators for that view.

After selecting that you run your project in a device and it will work correctly without missing any view.

Note: - It will work for both portrait and landscape modes. And mainly you don't have to give constraints for the middle view.

IMHO, best working solution is using "Proportional Height". For example, in my case, I finally ended with 2 views. Top one got 0.8 of height of superview, bottom - 0.2. It's not perfect solution, but you can still benefits from autolayout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!