iPhone X how to handle View Controller inputAccessoryView?

前端 未结 15 2720
长情又很酷
长情又很酷 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:15

    I just created a quick CocoaPod called SafeAreaInputAccessoryViewWrapperView to fix this. It also dynamically sets the wrapped view's height using autolayout constraints so you don't have to manually set the frame. Supports iOS 9+.

    Here's how to use it:

    1. Wrap any UIView/UIButton/UILabel/etc using SafeAreaInputAccessoryViewWrapperView(for:):

      SafeAreaInputAccessoryViewWrapperView(for: button)
      
    2. Store a reference to this somewhere in your class:

      let button = UIButton(type: .system)
      
      lazy var wrappedButton: SafeAreaInputAccessoryViewWrapperView = {
          return SafeAreaInputAccessoryViewWrapperView(for: button)
      }()
      
    3. Return the reference in inputAccessoryView:

      override var inputAccessoryView: UIView? {
          return wrappedButton
      }
      
    4. (Optional) Always show the inputAccessoryView, even when the keyboard is closed:

      override var canBecomeFirstResponder: Bool {
          return true
      }
      
      override func viewDidLoad() {
          super.viewDidLoad()
          becomeFirstResponder()
      }
      

    Good luck!

提交回复
热议问题