iPhone X how to handle View Controller inputAccessoryView?

前端 未结 15 2646
长情又很酷
长情又很酷 2020-11-30 00:43

I have a messaging app that has the typical UI design of a text field at the bottom of a full screen table view. I am setting that text field to be the view controller\'s

相关标签:
15条回答
  • 2020-11-30 01:28

    I'm just add safe area to inputAccessoryView (checkbox at Xcode). And change bottom space constraint equal to bottom of safe area instead of inputAccessoryView root view bottom.

    Constraint

    And result

    0 讨论(0)
  • 2020-11-30 01:30

    This is a general issue with inputAccessoryViews on iPhone X. The inputAccessoryView ignores the safeAreaLayoutGuides of its window.

    To fix it we have to manually add the constraint in your class when the view moves to its window:

    override func didMoveToWindow() {
        super.didMoveToWindow()
        if #available(iOS 11.0, *) {
            if let window = self.window {
                self.bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1.0).isActive = true
            }
        }
    }
    

    PS: self here is referring to the inputAccessoryView.

    I wrote about it in detail here: http://ahbou.org/post/165762292157/iphone-x-inputaccessoryview-fix

    0 讨论(0)
  • 2020-11-30 01:35

    The simplest answer (with just one line of code)

    Simply use a custom view that inherits from UIToolbar instead of UIView as your inputAccessoryView.

    Also don't forget to set that custom view's autoresizing mask to UIViewAutoresizingFlexibleHeight.

    That's it. Thank me later.

    0 讨论(0)
提交回复
热议问题