繁体   English   中英

自定义uiview作为inputview迅速

[英]custom uiview as inputview swift

我有一个自定义的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
}

在同一个viewcontroller中声明了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()函数中的inputview的布局?

谢谢

这是你想要的吗?

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM