调整inputAccessoryView的大小时如何解决“ API错误:返回0宽度,假设UIViewNoIntrinsicMetric?”]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整inputAccessoryView的大小时如何解决“ API错误:返回0宽度,假设UIViewNoIntrinsicMetric?”]相关的知识,希望对你有一定的参考价值。
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
当我运行模拟器时,一切看起来都很不错,但是当我点击UITextField时,控制台会报告此错误:
API error: <_UIKBCompatInputView: 0x7fc725f20520; frame = (0 0; 0 0); layer = <CALayer: 0x60000232e6c0>> returned 0 width, assuming UIViewNoIntrinsicMetric
我已经尝试将intrinsicContentSize
覆盖为非零值,例如CGSize(width: 50, height: 50)
,但仍然收到此错误。我该如何解决?
我从这里粘贴了代码示例:https://stackoverflow.com/a/46510833/784637到新的Single View App中,将inputAccessoryView固定在屏幕底部:类ViewController:...
以上是关于调整inputAccessoryView的大小时如何解决“ API错误:返回0宽度,假设UIViewNoIntrinsicMetric?”]的主要内容,如果未能解决你的问题,请参考以下文章