我有一个自定义输入视图:
func datePickerInputView() {
let screenWidth = UIScreen.mainScreen().bounds.width
let dateView = UIView(frame: CGRectMake(100, 100, screenWidth, 160))
datePicker = UIDatePicker(frame: CGRectMake(0, 0, screenWidth, 160))
datePicker.datePickerMode = .Date
datePicker.backgroundColor = UIColor.whiteColor()
dateView.addSubview(datePicker)
inputTextField.inputView = dateView
let inputAccView = UIView(frame: CGRectMake(100, 100, 100, 88))
let topMessage = UILabel(frame: CGRectMake(0, 0, screenWidth, 44))
topMessage.textAlignment = .Center
topMessage.text = "My Custom datePicker Keyboard"
topMessage.backgroundColor = UIColor.blackColor()
topMessage.textColor = UIColor(netHex: 0xFFAE00)
topMessage.font = UIFont.boldSystemFontOfSize(17)
topMessage.adjustsFontSizeToFitWidth = true
inputAccView.addSubview(topMessage)
let doneButton = UIButton (frame: CGRectMake(0, 45, (screenWidth / 2) - 1, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "dateUpdated", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(doneButton)// = doneButton
let cancelButton = UIButton (frame: CGRectMake((screenWidth / 2) + 1, 45, (screenWidth / 2) - 1, 44))
cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
cancelButton.addTarget(self, action: "didTapView", forControlEvents: UIControlEvents.TouchUpInside)
cancelButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(cancelButton)// = cancelButton
let whitStripe = UIView(frame: CGRectMake((screenWidth / 2) - 1, 48, 2, 36))
whitStripe.backgroundColor = UIColor.whiteColor()
inputAccView.addSubview(whitStripe)
inputTextField.inputAccessoryView = inputAccView
}
inputTextField 和 datePicker 在同一个视图控制器中声明,当我想要显示这个 datePickerView 时,我只运行这两个代码:
self.datePickerInputView()
self.inputTextField.becomeFirstResponder()
doneButton 的功能是:
func dateUpdated() {
//Run required code
inputTextField.resignFirstResponder()
}
但在我看来,我并没有真正使用 inputTextField。我把它藏起来了。但是当我单击按钮时,我需要它来调用 inputView,因为我找不到为按钮分配 inputview 的方法。
我在我想用 datePicker 显示这种类型的键盘的所有视图控制器中实现了这一点。
但我想创建一个单独的 UIView 子类,它将实现这个键盘视图,并在我想显示这个键盘布局时调用它,而不是在每个需要它的 viewController 中这样做。
我寻找自定义键盘,但我发现的大部分内容都实现了 xib。
无论如何我可以有一个 UIView 的子类,在需要时可以调用的函数 datePickerView() 中的输入视图的布局是否如上?
谢谢