打開視圖而不觸摸任何地方時,如何使游標閃爍。我目前正在使用 becomeFirstResponder() 我的文本欄位成為真正的第一回應者,但是當視圖打開時,閃爍不會閃爍。為什么?
我的代碼在這里。
import UIKit
class ViewController: UIViewController {
let resultTextField: UITextField = {
var myField = UITextField()
myField.textColor = .blue
myField.font = .systemFont(ofSize: 36)
myField.textAlignment = .right
myField.borderStyle = .roundedRect
return myField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(resultTextField)
resultTextField.becomeFirstResponder()
resultTextField.inputView = UIView()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let myWidth = self.view.frame.width
let myHeight = self.view.frame.height
resultTextField.frame = CGRect(x: (myWidth - (myWidth / 1.15))/2,
y: myHeight / 7.5,
width: myWidth / 1.15,
height: myHeight / 15)
}
}
謝謝
uj5u.com熱心網友回復:
我同意@Luca,嘗試在viewDidAppear或 layoutSubviews 函式中呼叫 becomeFirstResponder。viewDidLoad()僅表示您的 UIViewController 已加載到記憶體但尚未顯示。驗證這一點的簡單方法是實作 UITextField 的子類并覆寫caretRect回傳游標框架的子類。設定斷點并測驗代碼...
class CustomTextField: UITextField {
override func caretRect(for position: UITextPosition) -> CGRect {
/// set a breakpoint and you will notice that it's not called with the viewDidLoad() of your UIViewController
return super.caretRect(for: position)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483973.html
上一篇:SWIFT無序函式引數
