ios - UITextField的透明背景

标签 ios swift

我编写了一个 UITextField 使其在左上角和右上角变圆,但文本字段的背景现在全黑了

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "signupbackground.jpg")
self.view.insertSubview(backgroundImage, at: 0)
self.signupButton.layer.cornerRadius = 5

let rectShape = CAShapeLayer()
rectShape.bounds = self.userEmailTextField.frame
rectShape.position = self.userEmailTextField.center
rectShape.path = UIBezierPath(roundedRect: self.userEmailTextField.bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath

self.userEmailTextField.layer.backgroundColor = UIColor.black.cgColor
//Here I'm masking the textView's layer with rectShape layer
self.userEmailTextField.layer.mask = rectShape

如何使左上角/右上角的半径相同但文本字段完全可见?

最佳答案

您正在将图层的背景颜色设置为黑色:

self.userEmailTextField.layer.backgroundColor = UIColor.black.cgColor

请尝试使用 UIColor.clear.cgColor,您可能希望在绘制 mask 后设置它:

self.userEmailTextField.layer.mask = rectShape
self.userEmailTextField.layer.backgroundColor = UIColor.clear.cgColor

关于ios - UITextField的透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39908455/

相关文章:

Swift 4 按键访问元组

objective-c - 从 Swift 错误调用 objective-c 方法

ios - 带参数的 Swift 传递闭包

iOS UITableView 意外添加边距

ios - 更新后 iPhone 6 上的 Storyboard大小

ios - UIStackView 动态地在彼此旁边添加 2 个标签,没有额外的间距

Swift 中的 iOS 简单 SQLite Select 语句

ios - 为什么标签栏消失了?

ios - 在模拟器和本地设备上获取不同的时间格式 - Swift iOS

ios - API流程阻塞图和交互