闪烁的光标默认为Swift

发布于 2025-02-03 03:57:00 字数 1110 浏览 4 评论 0原文

当视图打开而无需触摸任何地方时,如何使光标闪烁。我目前正在使用beadfirstresponder()我的文本字段真正成为第一响应者,但是当视图打开时,眨眼就不会闪烁。为什么?

我的代码在这里。

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)
    }
}

谢谢

How can I make cursor blink when the view is opened without touching anywhere. I am currently using becomeFirstResponder() My text field becomes really first responder but when the view is opened, blink does not blink. Why?

My code is here.

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)
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-02-10 03:57:00

我同意@luca的观点,请尝试在 viewDidappear 或Layoutsubviews函数中调用beadfirstresponder。 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)
   }
}

I agree with @Luca, try calling becomeFirstResponder in viewDidAppear or in your layoutSubviews function. viewDidLoad()only means that your UIViewController has been loaded to memory but is not displayed yet. Easy way to verify this is implementing a subclass from UITextField and override the caretRect which returns the cursor's frame. Set a breakpoint and test the code ...

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)
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文