-1



I have huge issues when trying to use a custom view as a input keyboard for a text field.
The setup is pretty simple (for testing) : I have a UIView with one button, half the size of the screen and a text field somewhere above it.
I set up a dummy view to be the input view of the text field like so let dummyView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
Also I added the following setting on viewDidAppear testView.frame.origin.y = view.bounds.height. This is of course ignored as the view appears on screen (putting it on viewDidLoad leads to same behaviour). However, putting it in viewdidLayoutSubviews leads to a very strange behaviour, the view does not appear on screen, but trying to animate to, let's say a 380 origin.y does nothing.
The best part is yet to come: if I hide the view, then the slide up animation (code bellow)

UIView.animateWithDuration(0.6, animations: {
            self.testView.frame = self.whereToBe!
            }, completion: {
                finished in
                if finished {

                }
        })

works just fine. However, trying to slide it down again and resigningFirstResponder on the text field on button press (code bellow)

UIView.animateWithDuration(0.6, animations: {
            //self.testView.frame = self.whereToBe!
            self.testView.frame.origin.y = self.view.frame.height

            }, completion: {
                finished in
                if finished {
                    self.view.endEditing(true)
                    //self.inputField.resignFirstResponder()
                    self.testView.hidden = true
                }
        })

shows a fascinating wrong behaviour: the view slides from top to its current position. However, on second press on the button, it works just fine, it slides down BUT it reappears on screen after the animation has completed.
Can you please help me?

Razvan Soneriu
  • 587
  • 3
  • 7
  • 23

1 Answers1

0

Use the view as the inputView of the textfield by simply setting

self.textField.inputView = self.pickerView;

and avoid all these custom animations.

erenkabakci
  • 442
  • 4
  • 15
  • Ah my bad, I forgot to say that i already tried this, not working with this error : Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller: should have parent view controller: but requested parent is:' – Razvan Soneriu Mar 17 '15 at 12:26
  • Are you subclassing this view from UIView or UIViewController ? – erenkabakci Mar 17 '15 at 12:59
  • Its a simple uiview designed in interface builder with some buttons, which is set up in the general screen view. I didnt create a class specifically for it. nor did i create it someplace else as a view which i call programmatically. – Razvan Soneriu Mar 17 '15 at 13:36
  • Did you try this solution ? http://stackoverflow.com/questions/24029113/error-when-adding-input-view-to-textfield-ios-8 – erenkabakci Mar 17 '15 at 14:27
  • Yes, but if I remove it from superView it looses all constraints, most important of which is the height, which is replaced by the standard keyboard height which I don't want to happen – Razvan Soneriu Mar 17 '15 at 15:25