这仅涉及 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
它只是将其添加到标准边框中。
看到这里(点击放大)
如果有办法关闭
关闭神秘层(或draw
?),那将是一个解决方法,但我就是找不到关闭它的方法。 (如果你隐藏它,“他们”会在几帧后再次打开它!我不知道如何检查源代码以了解发生了什么。)注意,来自故事板...
borderStyle = .roundedRect
(如果您只是实例化文本字段,则为
.none
),这会进一步增加混乱。
.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"
}
}
结果:
编辑
也许尝试一下?
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"
}
}
输出:
在 iOS17 UIKit 中真的有任何方法可以更改 iOS17 UITextField 的边框颜色吗?没有。
你有两种半的可能性:
复制它们的边框并覆盖精确匹配,您可以根据需要着色。 (但请注意,如果 UIKit 在应用程序运行期间改变了其厚度、颜色等任何质量{可能是动画、状态更改或任何原因},您将不会知道这一点,并且外观也会发生变化。 )
///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
}
}