TPKeyboardAvoiding icon indicating copy to clipboard operation
TPKeyboardAvoiding copied to clipboard

TPKeyboardAvoiding with fullscreen UITextView doesn't work correctly

Open gcastaldi opened this issue 12 years ago • 6 comments

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

gcastaldi avatar Oct 24 '13 03:10 gcastaldi

I have the same issue with my project. Please help!

JamalK avatar Dec 22 '13 19:12 JamalK

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.

YSDC avatar Jan 19 '14 13:01 YSDC

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?

jungchris avatar Nov 16 '14 20:11 jungchris

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.

haojianzong avatar Aug 05 '15 03:08 haojianzong

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 '>='.

jungchris avatar Aug 05 '15 15:08 jungchris

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)
])

LeoSnek avatar Oct 14 '20 11:10 LeoSnek