1

I am experiencing an issue with the inputAccessoryView after ANY other view has been rotated. For example: 1) go to view A and rotate to landscape 2) then rotate back to portrait.
3) go to view B

in view B I have the below code to assign an inputAccessoryView to a UITextField However, after I rotate any view in the app, the accessory view always attaches to the top of the screen and not the top of the keyboard. If I don't rotate any view the accessory view attaches to the top of the keyboard as expected.

Here is the code used to create and assign the inputAccessoryView, it is called from the viewDidLoad

func addDoneButtonOnKeyboard(){

    let doneToolbar = UIToolbar(frame: CGRect(x:0, y:0, width:320, height:50))
    doneToolbar.barStyle = UIBarStyle.blackTranslucent
    doneToolbar.isTranslucent = true

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace,
                                    target: nil,
                                    action: nil)

    let doneButton = UIBarButtonItem(title: "Done",
                                     style: UIBarButtonItemStyle.done,
                                     target: self,
                                     action: #selector(MyVC.doneButtonAction))

    doneToolbar.setItems([flexSpace,doneButton], animated: false)
    doneToolbar.sizeToFit()
    doneToolbar.autoresizingMask=UIViewAutoresizing.flexibleWidth

    self.myTextFild.inputAccessoryView = doneToolbar
}

I have searched SO but did not see any other questions/answers similar....

rmp
  • 3,503
  • 1
  • 17
  • 26
  • http://stackoverflow.com/questions/8140520/how-should-i-get-my-inputaccessoryview-to-resize-when-rotating-device check this might give you a hint how to observe the orientationchange and adjust the frame. –  Feb 03 '17 at 18:47
  • @Sneak I have tried resizing the frame as was suggested is several SO posts but that didn't solve the problem. – rmp Feb 03 '17 at 18:52
  • if you have adjusted the frame size and position on device rotations correctly then I don't know really where the problem could be, maybe you should update the question with your methods for resizing and repositioning and how you observe the device rotation when doing so. –  Feb 03 '17 at 18:56
  • @Sneak the issues isn't with rotation of the view that contains the accessory view, that view is fixed to portrait. The issues occurs whenever a different view is rotated and then I go to the view with the accessory view. So rotation is not even allowed on the view in question. – rmp Feb 03 '17 at 19:00
  • Ah I see, I did some searching for you this is what I found maybe helps you http://stackoverflow.com/questions/32550036/make-custom-view-stick-to-top-of-the-keyboard-while-rotating-device .. however, thats all I can come up with, GL –  Feb 03 '17 at 19:06
  • 1
    @Sneak thanks for the effort, I did see this too but still no solution.... – rmp Feb 03 '17 at 19:10

1 Answers1

4

I my case the inputView was a datePicker and I had the same problem. The solution for me was:

textFieldDate.inputView?.autoresizingMask = [.flexibleHeight, .flexibleWidth]

Not sure if this helps for you as well, cause you are using a keyboard as inputView but maybe it helps others with datePicker problems.

tuvok
  • 679
  • 5
  • 16