I'm trying to achieve similar positioning behavior as the bottom text input bar in Apple's Messages app.
I have tried many approaches, searched high and low and there are many similar questions but none have been satisfactory.
To specify:
- There is a
UIToolbar
at the bottom of the view
- The toolbar is to follow the keyboard as the keyboard appears and disappears
- The toolbar should stay on top of the keyboard when the keyboard is visible
- When the keyboard is hidden, the toolbar stays ("docked") at the bottom of the view
The proposed solutions:
Manually animate the toolbar in response to keyboard appearance notifications
This solution does not meet a special case of the second requirement (the toolbar is to follow the keyboard as the keyboard appears and disappears):
- In iOS 7,
UIScrollViewKeyboardDismissMode
was introduced. It enables an interactive gesture for dismissing the keyboard. As the the user pans past the top edge of the keyboard, the keyboard frame gradually follows. This solution does nothing to accomodate this behavior and just leaves the toolbar stranded at it's animated position.
In addition, this solution also fails to fulfil a special case of the third requirement (the toolbar should stay on top of the keyboard when the keyboard is visible):
- Rotation. This solution calls for additional, annoyingly extraneous code (as we will see in the next proposed solution) to rotate the toolbar in response to device rotation.
Another issue with this solution:
- Keyboard height. With this solution, the toolbar it is not assumed to be part of the height of the keyboard, so additional code must be written to support proper insetting of content.
Next proposed solution:
Use UIResponder
's inputAccessoryView
This solution seems to be the way Apple intended to support this kind of behavior, as it solves all the shortcomings of manually animating the toolbar. But this solution completely misses the fourth requirement (when the keyboard is hidden, the toolbar stays ("docked") at the bottom of the view).
It seems the solution is to use UIResponder
's inputAccessoryView
, but somehow making the inputAccessoryView
not move below the view. I'm looking for clean code to accomplish this. There are elaborate, almost noble, attempts elsewhere, but as mentioned, they do not meet the requirements.
In my particular case, I am looking to use UINavigationController
's toolbar which entails additional issues as this is not intended behavior for UINavigationController
. No matter, I'm willing to introduce some hacky fixes to accomplish that.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…