Learn Courses My Dashboard

How to make keyboard avoid UITextView

Hey Chris, I’m using the UITextView in my app and I’m struggling to keep the cursor above the keyboard as I’m editing my text view. Is there an easy way to fix this because I can’t see what I’m typing in the app as the keyboard does not keep up with my typing? I’m sorry if this is confusing but it would be great if you could give me a hand!

An example of this is in the apple notes app, no matter how many times you enter a new line, the cursor will always be above the keyboard. Hopefully, that helps you visualize it!

Welcome to the community!!

Thai is something that seems like it should be easy, but it isn’t sadly

Easiest way is to use a third party package

Otherwise if you want to code it yourself you can start looking at this blog post

Or searching “keyboard avoid in swift”

Haha there’s lots of tutorials cause we all have to go through this!

(Honestly on my app I didn’t fully solve it, but it still works :joy:)

1 Like

Hey, thanks for responding, I was thinking of another way to work around this issue. For example, could there be a way to raise the UITextView when the keyboard is present by changing the constraints programmatically? I noticed that if I changed the UI to make the textView be above the keyboard no matter what, when the text reached the end of the textView it looked fine. However, if I do this, the UI of my app doesn’t look very appealing. So I was wondering if there could be a way to update the constraints of the UITextView when the keyboard opens and then change them again when the keyboard goes away? Thanks!

I think it would work perfectly if I am able to programmatically change the bottom vertical constraint of my stackview when the keyboard opens. Would you have any idea on how to do this?

It should work if I can somehow just change the stack view vertical constraint; Bottom Space to Superview equals to ___ and change that value programmatically. But I’m not too sure how to do this aha

Can you show me what your app looks like before they keyboard comes up and after it’s there?

(At this current moment)

1 Like

I’m not sure how to do that on my computer but basically, I have this screen that contains a basic UIView which is in red, and inside is a UITextView which has the following constraints in relation to the UIView. I was wondering if it would be possible for me to find a way to programmatically adjust the bottom constraint which I have highlighted so that when I activate the keyboard the constraint value will increase and then when I deactivate the keyboard the constraint will go back to what it originally was. I have already set up the texViewDidBeginEditing functions.

So I was thinking I could implement code in these functions using the NSLayoutConstraint. However, I do not really know how these work. (PS. the stack_view and tile_view are just temp names I have made when I linked the UIView and the UITextView to my swift file as @IBOutlet weak variables.)

But this doesn’t seem to change anything when I run the program and I think it has something to do with the fact I haven’t initialized the items correctly in the NSLayoutConstraint. If you have any idea on how I could potentially fix this so the constraints do update it should work! Hope this makes any sense :joy:

the textView is stored in a stackView by the way so the bottom constraint has to do with the UIView and the UIStackView

I meant take a screenshot with Cmd + Shift + 4 to show what your app currently looks like

You can create a constraint and activate/deactivate it programmatically, but what you’ve written so far isn’t how you should do that.

But is the textView actually defined in a storyboard file?

1 Like

yes, it is defined in the storyboard. I actually managed to get this to work by updating the constraint when I activate the keyboard using the NSLayoutConstraint. However, now my issue is that when I deactivate the keyboard, it will change the constraint by deactivating/activating the constraint and I’m not sure why as it worked to make the constraint when the view loads and also when I activate the keyboard.

Hello @heathr

Maybe this can help you solve your problem.

1 Like

wasn’t quite what I wanted it to do but thanks!

I managed to get it to work perfectly. Thanks for the help anyway! I’m still getting used to learning Swift and it’s great to see there is a helpful community that is happy to help newbies like me learn!

1 Like

Glad you found a way! (There’s always a million ways to do something with programming)

You can post it here and mark it as the solution if you like