我想从@IBInspectabale 创建一个键盘选择
如何实现这一目标
我正在创建一个视图,在其中插入一个 ImageView 和一个 TextField,现在我正在将此自定义视图类创建为 @IBDesignable 并创建 @IBInspectable 元素。
我成功地创建了侧面图像和占位符元素,但现在我正在尝试创建键盘类型但面临问题。
代码剪断:`导入UIKit
@IBDesignable 类 CustomTextField: UIView,UITextFieldDelegate {
//custom view from the XIB file
var view: UIView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var imageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CustomTextField", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(view);
}
@IBInspectable var sideImage: UIImage? {
get {
return imageView.image
}
set(sideImage) {
imageView.image = sideImage
}
}
@IBInspectable var placeHolderText: String? {
get {
return textField.placeholder
}
set(placeHolderText) {
textField.placeholder = placeHolderText
}
}'
以上所有工作正常,但以下对我不起作用:
@IBInspectable var keyboard: UIKeyboardType? {
get{
return UIKeyboardType(rawValue: textField.keyboardType.rawValue)
}
set(keyboard){
textField.keyboardType = keyboard!
}
}
}
我通过创建枚举来尝试它,但它没有给我任何结果。