我已经建立了一个 UITextField
以便将拾取器视图显示为该键盘的输入视图。我希望拾取器视图有圆角,但圆角下的区域仍然显示背景色。pickerView.layer.masksToBound
设置为true。
我的代码。
dummyTextField.delegate = self
pickerView.delegate = self
pickerView.dataSource = self
pickerView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 150)
pickerView.backgroundColor = offWhite
pickerView.layer.cornerRadius = 15
pickerView.layer.borderWidth = CGFloat(borderWidth)
pickerView.layer.borderColor = blue.cgColor
pickerView.layer.masksToBounds = true
pickerView.clipsToBounds = true
dummyTextField.inputView = pickerView
dummyTextField.inputView?.clipsToBounds = true
let pickerToolBar = UIToolbar()
let pickerDoneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(pickerDone))
pickerDoneButton.tintColor = blue
//I also added the toolbar here
self.window.addSubview(dummyTextField)
使用到 clipToBounds()
如果你的pickerView在输入视图中,那么在输入视图中调用这个方法。
pickerView.clipToBounds = true
textField.inputView?.clipsToBounds = true
将此值设置为true会使子视图被剪切到接收器的边界上。如果设置为false,则子视图的帧延伸到接收器的可见边界之外的部分不会被剪切。默认值为false。