This is about iOS 17 only. The only way I could hack around and achieve something like this was
- find the actual, real, border, get its real values
- sadly just add a fake on top of that layer once found, perfectly replicating it
so
///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
}
}
This is about iOS 17 only. Am I missing something really obvious?
This is about iOS 17 only. Even more issues on this time waste ...
- Bizarre: take my code above, instead of adding the fake layer, just change the color of the found layer. You'd think! I cannot for the life of me figure out where in the view cycle they set it. Any place I changed the color, "they" would change it back!! The only way I could make it stick was with a timer, for goodness sake - give it a go.
Am I missing something really obvious? - is there a way to change the border color of a UITextField as is?
It looks like if you set the border style on a UITextField these days,
override func common() {
super.common()
borderStyle = .roundedRect
layer.borderWidth = 1.0 / UIScreen.main.scale
layer.cornerCurve = .circular
layer.cornerRadius = 10
It just adds that to the standard border.
seen here
(tap for large)
If there was a way to turn off the mystery layer (or draw
?), that would be a work around, but I just cannot find a way to turn it off. (If you hide it, "they" turn it on again after a few frames! I don't know how to examine the source code to see what's going on.)
NB, from storyboard ...
If you're investigating this recall that is it begins from storyboard they set borderStyle = .roundedRect
(it's .none
if you just instantiate a text field), adding further confusion.
borderStyle = .none
instead ofborderStyle = .roundedRect
and give rounded border manually... Sorry, little confused what you want to do... OR simply you want to change color of textfield border?