自定义uiview作为inputview swift

时间:2016-06-23 23:26:33

标签: ios swift uiview inputview

我有一个自定义输入视图:

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在同一个viewcontroller中声明,当我想要显示这个datePickerView时,我只运行这两个代码:

self.datePickerInputView()
self.inputTextField.becomeFirstResponder()

和doneButton的功能是:

func dateUpdated() {
    //Run required code
    inputTextField.resignFirstResponder()
}

但我在视图中并没有真正使用inputTextField。我把它隐藏起来了。但是当我点击一个按钮时,我需要它来调用inputView,因为我找不到为按钮分配输入视图的方法。

我在所有的viewcontrollers中实现了这个,我希望用datePicker显示这种类型的键盘。

但我想创建一个单独的UIView子类来实现这个键盘视图,并在我想要显示这个键盘布局时调用它,而不是在需要它的每个viewController中执行此操作。

我寻找自定义键盘,但我发现的大多数都实现了xib。

有没有我可以拥有UIView的子类,它在函数datePickerView()中为inputview做了如何布局,可以在需要时调用它?

谢谢

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

extension UIViewController {
    func datePickerInputView(inout datePicker: UIDatePicker, inputTextField: UITextField) {
        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
    }
}