TPKeyboardAvoiding
TPKeyboardAvoiding copied to clipboard
TPKeyboardAvoiding with fullscreen UITextView doesn't work correctly
Hi, I've created a simple project with a main view that contains: UIView -> TPKeyboardAvoiding -> UITextView When you write in the UITextView the cursor goes below the keyboard like I'm using a simple UIScrollView. You can download it at https://github.com/gcastaldi/AvoidKbHelloWorld.git.
Thanks. Giannandrea
I have the same issue with my project. Please help!
I also have an issue with textview support.
After some textfield I have a textview. It appears that doing multiple return line in the textview result in a bad state where the scrolling view became shorter, and i cannot reach the end of my table view. Even if i try to disable return line in my textview the problem still exist.
I've come across what seems like a similar problem with a full view UITextView set up with standard hierarchy of ViewController -> UIScrollView (TPKeyboardAvoidingScrollView) -> UITextView. I did not see the method that is able to recognize in what area of a UITextView the user is interacting, but I didn't look at all the classes. Has anyone been able to resolve this issue since it was opened more than a year ago?
I have come across this issue as well, but it turns out be a stupid mistake. I have used a wrong class for my scrollView. There are a few available classes from this repo:
- TPKeyboardAvoidingScrollView
- TPKeyboardAvoidingTableView
- TPKeyboardAvoidingCollectionView
It is easy to mis-type the class name in storyboard, and the compiler will not complain about it. So please double check it.
As haojianzong mentions double check the class for the scrollview. Adding a "container" view in the case of a ScrollView with constraints set to the superview is also a good practice as follows: ViewController -> UIScrollView (TPKeyboardAvoidingScrollView) -> UIView (container for UI elements) -> UITextView. If you want vertical scrolling set the height constraint to be '>='.
I have got this working nicely, including respecting safe areas, so wanted to share some code to help others. This uses the same structure as mentioned by @jungchris above.
Firstly make sure to set the following properties:
scrollView.contentInsetAdjustmentBehavior = .always
textView.isScrollEnabled = false
The set up the constraints as follows:
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
containerView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
containerView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
containerView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
containerView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor),
textView.topAnchor.constraint(equalTo: containerView.topAnchor),
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor)
])