iPhone X如何处理View Controller inputAccessoryView? [英] iPhone X how to handle View Controller inputAccessoryView?
问题描述
我有一个消息传递应用程序,它在全屏幕表视图的底部具有文本字段的典型UI设计。我将该文本字段设置为视图控制器的 inputAccessoryView
并调用 ViewController.becomeFirstResponder()
以获取字段显示在屏幕的底部。
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 inputAccessoryView
and calling ViewController.becomeFirstResponder()
in order to get the field to show at the bottom of the screen.
我知道这是Apple推荐的实现此UI结构的方式,它在经典设备上运行完美但是当我在iPhone X模拟器上测试我注意到使用这种方法,文本字段不尊重新的安全区域。文本字段显示在主屏幕指示器下方屏幕的最底部。
I understand this is the Apple recommended way of accomplishing this UI structure and it works perfectly on "classic" devices however when I test on the iPhone X simulator I notice that using this approach, the text field does not respect the new "safe areas". The text field is rendered at the very bottom of the screen underneath the home screen indicator.
我查看了HIG文档,但没有发现有关视图控制器上的 inputAccessoryView
。
I have looked around the the HIG documents but haven't found anything useful regarding the inputAccessoryView
on a view controller.
这很难,因为使用这种方法我实际上并没有直接控制任何约束,我只是设置 inputAccessoryView
并让视图控制器从那里处理UI。因此,我不能将该字段限制在新的安全区域。
It's difficult because using this approach I'm not actually in control of any of the constraints directly, I'm just setting the inputAccessoryView
and letting the view controller handle the UI from there. So I can't just constrain the field to the new safe areas.
有没有人找到关于此的良好文档或知道在iPhone X上运行良好的替代方法?
Has anyone found good documentation on this or know of an alternate approach that works well on the iPhone X?
推荐答案
< h1> inputAccessoryView
和iPhone X上的安全区域
-
键盘时如果不可见,
inputAccessoryView
固定在屏幕的最底部。没有办法,我认为这是预期的行为。inputAccessoryView
and safe area on iPhone Xwhen the keyboard is not visible, the
inputAccessoryView
is pinned on the very bottom of the screen. There is no way around that and I think this is intended behavior.layoutMarginsGuide
(iOS 9 +)和safeAreaLayoutGuide
(iOS 11)视图属性设置为inputAccessoryView
都尊重安全区域,即在iPhone上X:the
layoutMarginsGuide
(iOS 9+) andsafeAreaLayoutGuide
(iOS 11) properties of the view set asinputAccessoryView
both respect the safe area, i.e on iPhone X :- 当键盘不可见时,家中的
bottomAnchor
帐户按钮区域 - 当显示键盘时,
bottomAnchor
位于inputAccessoryView $的底部c $ c>,这样它就不会在键盘上方留下任何无用的空间
- when the keyboard is not visible, the
bottomAnchor
accounts for the home button area - when the keyboard is shown, the
bottomAnchor
is at the bottom of theinputAccessoryView
, so that it leaves no useless space above the keyboard
工作示例:
import UIKit class ViewController: UIViewController { override var canBecomeFirstResponder: Bool { return true } var _inputAccessoryView: UIView! override var inputAccessoryView: UIView? { if _inputAccessoryView == nil { _inputAccessoryView = CustomView() _inputAccessoryView.backgroundColor = UIColor.groupTableViewBackground let textField = UITextField() textField.borderStyle = .roundedRect _inputAccessoryView.addSubview(textField) _inputAccessoryView.autoresizingMask = .flexibleHeight textField.translatesAutoresizingMaskIntoConstraints = false textField.leadingAnchor.constraint( equalTo: _inputAccessoryView.leadingAnchor, constant: 8 ).isActive = true textField.trailingAnchor.constraint( equalTo: _inputAccessoryView.trailingAnchor, constant: -8 ).isActive = true textField.topAnchor.constraint( equalTo: _inputAccessoryView.topAnchor, constant: 8 ).isActive = true // this is the important part : textField.bottomAnchor.constraint( equalTo: _inputAccessoryView.layoutMarginsGuide.bottomAnchor, constant: -8 ).isActive = true } return _inputAccessoryView } override func loadView() { let tableView = UITableView() tableView.keyboardDismissMode = .interactive view = tableView } } class CustomView: UIView { // this is needed so that the inputAccesoryView is properly sized from the auto layout constraints // actual value is not important override var intrinsicContentSize: CGSize { return CGSize.zero } }
这篇关于iPhone X如何处理View Controller inputAccessoryView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 当键盘不可见时,家中的