在 iOS17 UIKit 中,有没有办法更改/影响 iOS17 UITextField 的边框颜色(除了此处显示的 mangle 之外)?

问题描述 投票:0回答:2

这仅涉及 iOS 17。我可以绕过并实现这样的目标的唯一方法是

  • 找到实际的、真实的、边界,得到它的真实值
  • 遗憾的是,一旦发现,就在该层的顶部添加一个假的,完美地复制它

所以

///True™ iOS look of the border, and you can change the color
class FixedUITextField: UITextField {
    
    var completed: Bool = true {
        didSet {
            fakeLayer.borderColor = completed ? rememberDefaultColor : UIColor.red.cgColor
        }
    }
    
    private var rememberDefaultColor: CGColor? = UIColor.gray.cgColor
    
    private lazy var fakeLayer: CALayer = {
        let v = RepairedCALayer()
        for found in layer.sublayers ?? [] {
            if found.borderWidth > 0 {
                layer.insertSublayer(v, above: found)
                v.backgroundColor = found.backgroundColor
                v.borderColor = found.borderColor
                v.borderWidth = found.borderWidth
                v.cornerRadius = found.cornerRadius
                v.cornerCurve = found.cornerCurve
                print("found and covered")
                rememberDefaultColor = found.borderColor
                return v
            }
        }
        // defaults ICO disaster
        v.backgroundColor = UIColor.systemBackground.cgColor
        v.borderColor = UIColor.blue.cgColor
        v.borderWidth = 1.0 / UIScreen.main.scale
        v.cornerRadius = 4.0
        v.cornerCurve = .circular
        layer.addSublayer(v)
        return v
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        fakeLayer.frame = bounds
    }
}

这仅涉及 iOS 17。我是否遗漏了一些非常明显的东西?

这仅涉及 iOS 17。关于这种时间浪费的更多问题......

  • 奇怪:采用上面的代码,而不是添加假图层,只需更改找到的图层的颜色。你一定会想!我一生都无法弄清楚他们在视图周期中的哪个位置设置了它。任何我改变颜色的地方,“他们”都会把它改回来!我能让它坚持下去的唯一方法就是使用计时器,看在上帝的份上——试一试。

我是否遗漏了一些非常明显的东西? - 有没有办法按原样更改 UITextField 的边框颜色?


看起来如果你现在在 UITextField 上设置边框样式,

override func common() {
    super.common()
    borderStyle = .roundedRect
    layer.borderWidth = 1.0 / UIScreen.main.scale
    layer.cornerCurve = .circular
    layer.cornerRadius = 10

它只是将其添加到标准边框中。

enter image description here

看到这里

enter image description here

(点击放大)

如果有办法关闭

关闭神秘层(或draw

?),那将是一个解决方法,但我就是找不到关闭它的方法。 (如果你隐藏它,“他们”会在几帧后再次打开它!我不知道如何检查源代码以了解发生了什么。)

注意,来自故事板...

如果您正在调查此召回,那么它是从故事板开始的,他们设置了

borderStyle = .roundedRect

(如果您只是实例化文本字段,则为 
.none
),这会进一步增加混乱。

swift uitextfield calayer ios17
2个回答
1
投票
将文本字段的

.borderStyle

 设置为 
.roundedRect
,然后在文本字段的 
.layer
 本身上设置所需的边框属性:

class RedTextFieldVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() let tf = UITextField() tf.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tf) let g = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ tf.widthAnchor.constraint(equalToConstant: 240.0), tf.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0), tf.centerXAnchor.constraint(equalTo: g.centerXAnchor), ]) tf.borderStyle = .roundedRect tf.layer.borderColor = UIColor.red.cgColor tf.layer.borderWidth = 1.0 / UIScreen.main.scale tf.layer.cornerRadius = 4.0 tf.layer.cornerCurve = .circular tf.text = "Hello Red Border" } }
结果:

enter image description here


编辑

也许尝试一下?

class MyBorderedTextField: UITextField { override class var layerClass: AnyClass { CAShapeLayer.self } private var shapeLayer: CAShapeLayer { layer as! CAShapeLayer } override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } private func commonInit() { shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.strokeColor = UIColor.red.cgColor shapeLayer.lineWidth = 1.0 / UIScreen.main.scale } override func layoutSubviews() { super.layoutSubviews() // tweak cornerRadius to suit shapeLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 6.0).cgPath } override func textRect(forBounds bounds: CGRect) -> CGRect { // tweak the 7,6 insets to suit return bounds.insetBy(dx: 7.0, dy: 6.0) } } class RedTextFieldVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() let tf = UITextField() tf.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tf) let tf2 = MyBorderedTextField() tf2.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tf2) let g = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ tf.widthAnchor.constraint(equalToConstant: 240.0), tf.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0), tf.centerXAnchor.constraint(equalTo: g.centerXAnchor), tf2.widthAnchor.constraint(equalToConstant: 240.0), tf2.topAnchor.constraint(equalTo: tf.bottomAnchor, constant: 8.0), tf2.centerXAnchor.constraint(equalTo: g.centerXAnchor), ]) tf.borderStyle = .roundedRect tf.text = "Hello Plain Border" tf2.text = "Hello Red Border" } }
输出:

enter image description here


0
投票
答案是:“做不到”;这是一个解决方法

在 iOS17 UIKit 中真的有任何方法可以更改 iOS17 UITextField 的边框颜色吗?

没有。

你有两种半的可能性:

  1. 使用我在问题中给出的 mangle,它将精确地

    复制它们的边框并覆盖精确匹配,您可以根据需要着色。 (但请注意,如果 UIKit 在应用程序运行期间改变了其厚度、颜色等任何质量{可能是动画、状态更改或任何原因},您将不会知道这一点,并且外观也会发生变化。 )

  2. 非常简单地消除“他们的”边界并使用您自己的边界。这样做的唯一技巧是您必须设置文本矩形(本质上是在视觉上设置填充):


///Essentially, you can't CHANGE THE COLOR of the border in a UITextField. ///Here, we simply turn it off and recreate it strictly BY EYE. ///Don't expect this to match UITextField in any serious project. class ManualBorderUIITextField: UIITextField { override func common() { super.common() borderStyle = .none layer.borderWidth = 2.0 / UIScreen.main.scale // In 2024 iOS it's 1 pixel there, I like 2 layer.cornerCurve = .circular layer.cornerRadius = 4 layer.borderColor = UIColor.green.cgColor // Choice is yours. In most real projects you'd be changing it per your use/states etc } ///Note that very confusingly UITextField will do the intrinsic size for you backwards from this, you don't have to. override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.insetBy(dx: 7.0, dy: 8.0) // When you kill the border style this padding becomes zero so you have to do it manually // In 2024 iOS it's possibly ~7/~6 there but hard to say exactly } }


2.5。我想,如果您非常肛门,您可以在启动文本字段时执行“1”,记住所有值,然后以“2”方法使用它。 (不过,您不会知道“他们”在运行过程中理论上可能做出的任何更改;您只是像任何其他自定义控件一样构建自定义控件:/)

© www.soinside.com 2019 - 2024. All rights reserved.