7

I implemented an own inputAccessoryView within a VC that gets pushed by a UINavigationController.

This is how the VC looks like: Notice the inputAccessoryView at the bottom (the white bar with the button and a text field)

1]

When I swipe from the left to the right of the screen (in order to dismiss the current VC and go back), the inputAccessoryView moves down and disappears. In addition, if I stop the swipe gesture anywhere and let the current VC jump back (so that it won't be dismissed), the inputAccessoryView also moves down and disappears.

I attached another photo while moving: 2]

An another one after the VC jumped back:

3]

As you can see, the inputAccessoryView has disappeared.

My piece of code:

private final lazy var inputContainerView: UIView = {
    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
    [...]
    return containerView
}()
[...]


override var inputAccessoryView: UIView? {
    return inputContainerView
}

override var canBecomeFirstResponder: Bool {
    return true
}
j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38

1 Answers1

5

You may want to try calling

self.becomeFirstResponder() in viewWillAppearor viewDidAppear both of them will be trigged when that happens.

tpeodndyy
  • 369
  • 2
  • 3
  • Yup, that worked very well... xD Thanks a lot! Is there any simple explanation on what becomeFirstResponder() does? – j3141592653589793238 Mar 06 '18 at 17:02
  • 2
    `inputAccessoryView` will shown only when the object that its binding to `isFirstResponder` value is true. In this case that object is your viewController (that's why you override `canBecomeFirstResponder`). I'm not 100% sure but I think in this case when dragging action happen and cancelled, the first responder doesn't resigned back to this viewController properly... so we have to force it to happen. – tpeodndyy Mar 06 '18 at 17:17
  • `override func viewWillAppear(_ animated: Bool) { self.becomeFirstResponder() } ` yes this worked for me – torun Jul 07 '20 at 21:58