Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting interface when keyboard appears for UITextField or UITextView

Tags:

cocoa-touch

I have a table with each cell containing a label and a text field. Problem is that when i go to edit the last row, keyboard hides the lower portion of the table, and i can't see what is being typed. How can i move my interface above the keyboard so i see what is being typed?

Thanks, Mustafa

like image 436
Mustafa Avatar asked Dec 31 '08 10:12

Mustafa


People also ask

How do I change the type of the keyboard in uitextfield?

How To Change UITextField Keyboard Type In Xcode Attribute Inspector. Click the UITextField UI component in the Main.storyboard file to select it. Then click View —> Inspectors —> Show Attributes Inspector menu item at Xcode top menu bar. Scroll down to the Text Input Traits section, there are three drop-down lists related to the keyboard.

How to implement the textview function in UITextView?

Then you can implement the textView function with the below source code. // This function is called when you input text in the textView. print("textView.") // If user press return key in the keyboard. // Resign first responder from UITextView to close the keyboard.

How to hide the keyboard on UITextView’s return key?

// Resign the first responder from textField to close the keyboard. To hide the keyboard when the user presses the return key on UITextView ‘s keyboard. We can add the below code in the textView function. You should first make your ViewController class implement the UITextViewDelegate delegate protocol.

How to resign the first responder from textfield in uitextfield?

So we can add the below code in the textFieldShouldReturn function to make the UITextField’s keyboard resign the first responder when pressing the return key. // This function is called when you click return key in the text field. // Resign the first responder from textField to close the keyboard.


2 Answers

You'll want to register your viewController for UIKeyboardDidShowNotification and UIKeyboardWillHideNotification events. When you get these, you should adjust the bounds of your table; the keyboard is 170 pixels height, so just shrink or grow your table bounds appropriately, and it should properly adjust to the keyboard.

like image 52
Ben Gottlieb Avatar answered Jan 04 '23 04:01

Ben Gottlieb


This problem is complex depending on your UI scenario. Here I will discuss a scenario that where UITextField or UITextview resides in a UITableViewCell.

  1. You need to use NSNotificationCenter to detect UIKeyboardDidShowNotification event. see http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html. You need to shrink the UITableView frame size so that it occupies only the screen area that is not covered by the keyboard.

    1. If you tap a UITableViewCell, the OS will automatically position the cell within the viewing area of UITableView. But it does not happen when you tap a UITextView or UITableViewCell even though it resides in a UITableViewCell.

You need to call

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

to programmatically "tap" the cell.

If you implement both points, you will see the UITextView/Field position right above the keyboard. Bare in mind that the UITableViewCell where the UITableView/Field resides cannot be taller than the "not covered" area. If this is not the case for you, there is a different approach for it but I will not discuss here.

like image 34
Wayne Lo Avatar answered Jan 04 '23 03:01

Wayne Lo