我想实现以下UI界面:
这是我编写的代码:
![enter image description here](https://istack.dev59.com/Kqfbf.webp)
class RoundTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
customize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
customize()
}
func customize() {
apply(shadow: true)
}
func apply(shadow: Bool) {
if shadow {
borderStyle = .none
layer.cornerRadius = bounds.height / 2
layer.borderWidth = 1.0
layer.borderColor = UIColor.lightGray.cgColor
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 5.0)
layer.shadowRadius = 2
layer.masksToBounds = false
layer.shadowOpacity = 1.0
} else {
}
}
}
以下是问题细节:
- 情况1:对于上述代码,我得到了一个没有任何边框或阴影的textField。
- 情况2:如果我注释掉前两行,则会得到带有阴影效果的textField,但边框不是圆角的,边框变为默认的圆角边框。
borderStyle = .none
layer.cornerRadius = bounds.height / 2
我该如何解决这个问题?
UITextField
来获得这种外观。更可能的是,您需要创建一个UIView
子类来设置边框、圆角和阴影,并具有UITextField
子视图。 - DonMag