0

I have a custom inputview as:

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 and datePicker are declared in the same viewcontroller and when I want this datePickerView to be displayed, I just run this two codes:

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

and the function for the doneButton is:

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

But I don't really use the inputTextField in my view. I have it hidded. But I need it to call inputView when I click a button, since I cannot find a way to assign an inputview for a button.

I implement this in all the viewcontrollers I want to display this type of keyboard with a datePicker.

But I wanted to create a separate UIView subclass that will implement this keyboard view and call it whenever I want to diplay this keyboard layout instead of doing this in every viewController that needs it.

I looked for custom keyboard but most of what I found implements a xib.

Is there anyway I can have a subclass of UIView, that does the layout for the inputview as above in the function datePickerView() that can be called when needed?

Thank you

tew
  • 261
  • 4
  • 13
  • kind of an odd question... I'm sure there is a way to do what you want, but I also did not get the full picture of what you want from the question. I guess I would have to see it in action. – Sethmr Jun 23 '16 at 23:32
  • I just want to have the datePickerInputView() in a public class possibly a subclass of UIView so that I can use it in other viewControllers instead of having the above code everywhere i need it. – tew Jun 24 '16 at 19:39

1 Answers1

0

Is this what you want?

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
    }
}
Sethmr
  • 3,046
  • 1
  • 24
  • 42