Why does my inputAccessoryView disappear when navigating backwards?
Asked Answered
C

1

8

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
}
Chazan answered 6/3, 2018 at 12:46 Comment(3)
Are you hiding tabbar when push to view controller?Gathers
Show your code for adding inputContainerViewGathers
I am hiding the tabbar. The code for adding the inputContainerView is already there. It‘s the accessory view. @JD.Chazan
A
5

You may want to try calling

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

Antoinetteanton answered 6/3, 2018 at 16:43 Comment(3)
Yup, that worked very well... xD Thanks a lot! Is there any simple explanation on what becomeFirstResponder() does?Chazan
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.Antoinetteanton
override func viewWillAppear(_ animated: Bool) { self.becomeFirstResponder() } yes this worked for meRabato

© 2022 - 2024 — McMap. All rights reserved.