na19
na19

Reputation: 97

iOS: How to adjust height of area covered by keyboard in iOS

Im curious about a feature in iOS. Please help me out here if you can.

Scenario: I am using a text box where name is entered. Its on lower half of the screen. Just below the text box is a label which displays the number of characters remaining(e.g.like in a twitter feed).

Problem: When i place the text box in upper half of the screen. both the text field and label are visible. But when I place them in lower half, the apple keyboard covers the label part. Is there a way where I control the area covered in such a way that the label below is also visible? I hope I have made myself clear enough. Thanks.

Upvotes: 4

Views: 2942

Answers (4)

Chitra Khatri
Chitra Khatri

Reputation: 1258

Here i have used delegate method for UITextView Same way you can do for UITextField

-Here in this code when user starts entering values in textview it makes your view's hight lesser then its original with animation

-When user Ends Entering values, it will make your view's size original size.

-(void)textViewDidBeginEditing:(UITextView *)textView
{

    CGRect frame = self.view.frame;
    frame.origin.y = -100;
    [self.view setFrame:frame];
    [UIView commitAnimations];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    [self.view setFrame:frame];
    [UIView commitAnimations];

 }

If you want to know about delegates this link helps you

Upvotes: 5

Ashish
Ashish

Reputation: 11

Add following to your viewDidLoad Method

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard) name:UIKeyboardWillHideNotification object:nil];

And after that-- Declare the following 2 methods in your .m file

-(void)showKeyboard {
    [UIView beginAnimations:@"slide" context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.transform = CGAffineTransformMakeTranslation(0, -100);
    [UIView commitAnimations]; }

-(void)hideKeyboard {
    [UIView beginAnimations:@"slide" context:nil];
    [UIView setAnimationDuration:0.1];
    self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    [UIView commitAnimations]; }

Upvotes: 1

egghese
egghese

Reputation: 2223

AFAIK You can't control the size of iOS native Keyboard, all you can and should be doing is, making them a subivew of a scroll view and scroll it up.

So the usual practice go something like this.

  • Subscribe to the Keyboard notification. UIKeyboardWillShowNotification
  • In the method which the Notification listener will be invoking, set the scrollView's content size accordingly and set the content offset.

    self.scrollView.contentSize = CGSizeMake(320, 267);
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, localKeyboardFrame.size.height, 0); 
    [self.scrollView scrollRectToVisible:<rect of view you want to scroll to> animated:YES];
    
  • Undo the changes when the keyboard hides, with the help of appropriate notification.UIKeyboardWillHideNotification

    self.scrollView.contentInset = UIEdgeInsetsZero
    

And here is iOS Human Interface Guide's explanation on it.

Upvotes: 2

Lithu T.V
Lithu T.V

Reputation: 20021

Well in that case you have to move the textbox when the keyboard pops up.You can have the notification registered to know when the keyboard pops up and a scrollview to scroll the whole content up the screen can do the job for you

See this question,It explains well how to manage something like this

Upvotes: 2

Related Questions