ios - iOS:旋转后 UITextField 的 inputAccessoryView 没有更新
问题描述
我写了一个自定义UITextField
,并在键盘上方添加了一个按钮inputAccessoryView
。
这是代码:
class CustomTextField: UITextField {
var button: UIButton?
var buttonContainer: UIView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
redraw()
}
override init(frame: CGRect) {
super.init(frame: frame)
redraw()
}
private func redraw() {
button = UIButton()
button?.setTitle("BUTTON TEST", for: .normal)
button?.backgroundColor = .red
let dividend: CGFloat = isPortraitOrientation ? 56 : 1
let divisor: CGFloat = isPortraitOrientation ? 326 : 18
let buttonHeight = ((UIScreen.main.bounds.width - 48) * dividend) / divisor
button?.frame = CGRect(origin: CGPoint(x: 24, y: 0), size: CGSize(width: UIScreen.main.bounds.width - 48, height: buttonHeight))
buttonContainer = UIView()
buttonContainer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: buttonHeight + 10)
buttonContainer?.backgroundColor = .clear
inputAccessoryView = buttonContainer!
buttonContainer?.addSubview(button!)
}
func updateView() {
redraw()
}
}
在控制器中:
class CustomViewController: UIViewController {
@IBOutlet weak var textField: CustomTextField!
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
self.textField.updateView()
}
}
}
当我第一次旋转屏幕,然后开始编辑文本字段时,两者都正确button
显示。buttonContainer
但是在旋转手机时它们没有正确重绘。
谢谢你的帮助。
解决方案
推荐阅读
- qt - QML TableView 实时刷新仅每 1 秒更新一次 UI
- javascript - 将对象数组中每个对象的键放入数组中:Javascript
- forms - Symfony 4 嵌入表单 - 仅嵌入部分
- java - 我面临声纳建议问题,即 Method 有 8 个参数,大于我的代码中授权的 7 个参数
- typescript - TypeScript - 迁移 JavaScript 函数
- python - 结构化流 Kafka 2.1->Zeppelin 0.8->Spark 2.4:spark 不使用 jar
- python - 如何使用 python 运行 Airtest 自动化项目 (.air)?
- mysql - 复合外键更新失败
- swift - 滞后于在本地服务器上运行的特定视频 (iOS)
- asp.net - 如何在不使用javascript的情况下检查动态复选框?