4

I have a viewController with an inputAccessoryView that is displayed as below:

override var inputAccessoryView: UIView? {
   get {
       return myInputView
   }
}

override var canBecomeFirstResponder: Bool {
   return true
}

I can easily show and hide this inputAccessoryView by using self.becomeFirstResponder() and self.resignFirstResponder()

The problem I have is when the viewController is first presented (instantiated from storyboard), the inputAccessoryView is visible. I can only dismiss it in viewDidAppear using self.resignFirstResponder(). This shows it moving down with an animation.

I am guessing that a viewController automatically becomes firstResponder when it is presented, but wondering how I can override that before viewWillAppear. There is no effect in putting the self.resignFirstResponder method in viewWillAppear.

Amit
  • 4,837
  • 5
  • 31
  • 46
alionthego
  • 8,508
  • 9
  • 52
  • 125
  • Couldn't you load and assign the inputAccessoryView in your view did load? that way it won't be loaded from the nib – Aatish Molasi Nov 18 '17 at 05:12
  • the problem is that the viewController is firstResponder and I have no way to undo that until the viewController is on screen. I need to assign the inputAccessoryView in the getter of the inputAccessoryView variable for it to work as I want. – alionthego Nov 18 '17 at 05:21
  • Ah, what about hiding it and unhiding it once it disappears. I know, seems hacky – Aatish Molasi Nov 18 '17 at 05:26
  • I have done a version of that. Basically I set a bool to indicate when I require the inputAccessoryView. in the getter if the bool is false, I return nil otherwise return the accessoryView. it works fine but I prefer to know how to control the viewController's firstResponder state and have a cleaner solution to this. – alionthego Nov 18 '17 at 05:28
  • 1
    @alionthego just add a bool property `var firstResponder = false` to your view controller and set it to true inside viewDidAppear method. and change canBecomeFirstResponder method to `override var canBecomeFirstResponder: Bool { return firstResponder }` – Leo Dabus Nov 18 '17 at 05:28
  • 1
    that is a workable solution but I also have the same problem when popping a pushed viewController back to this viewController. I could modify the firstResponder variable you suggested when pushing but it starts to get a bit messy. I will stick with the bool variable inside the inputAccessoryView getter. btw, coolest profile picture ever! got to put on some rush... – alionthego Nov 18 '17 at 05:34
  • @alionthego have you solved this problem ? – a.masri Oct 23 '18 at 06:01

0 Answers0