samedi 25 juin 2016

custom uiview as inputview swift


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

Aucun commentaire:

Enregistrer un commentaire