On my iPhone app i have a UIWebView with toolbars above an below it. the toolbar on the bottom contains a text box for the user to input some text. but when i click on the text box, the keyboard covers the bottom half of the screen.
how do i make it so that the toolbars stay above and below the webview but the the height of the webview shrinks for the keyboard to be displayed?
any guidance on this is appreciated.
thanks in advanced.
To shrink webView you need the following:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
CGFloat keyboardHeight = keyboardFrame.size.height;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
}
completion:NULL];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsZero;
}
completion:NULL];
}
In the case of additional subviews you should slightly change this code (add change of the frames for other subview)
You can implement UITextFieldDelegate in your UIViewController and set the UITextField delegate value to your controller and then implement textFieldDidBeginEditing and textFieldDidEndEditing methods in your controller to detect when editing starts/ends.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// in case you have more than one text fields in the same view
if(textField == self.YOUR_FIELD_NAME)
// change the web view height here, you can also animate it using UIView beginAnimations
CGRect frame = self.webView.frame;
frame.size.height = 200;
self.webView.frame = frame;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// do the opposite here
}
Related
I have the following view hierarchy. View->container->2 UITextfields and 1 button in containerView. The container is in the center of the screen. What I want to do is to move the container up when keyboard appears and UITextfield is behind the keyboard and move back to center when keyboard disappear. Here is the screenshot for the same.
What constraints do I need to change or do I need to add constraints in code ?
Get the frame of the container while keyboard show and update the new frame size. 'setTranslatesAutoresizingMaskIntoConstraints' is the right solution while updating frame of a view.It worked for me
#implementation ViewController
{
CGRect defaultFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark Notifications
- (void)keyboardWillShow:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
defaultFrame = self.ContentView.frame;
[self.ContentView setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.ContentView layoutIfNeeded];
CGRect contentInsets = CGRectMake(defaultFrame.origin.x, (keyboardSize.height), defaultFrame.size.width, defaultFrame.size.height);
[UIView animateWithDuration:0.5f
animations:^{
self.ContentView.frame = contentInsets;
}
completion:^(BOOL finished){
}
];
self.ContentView.frame = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self.ContentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[UIView animateWithDuration:0.5f
animations:^{
self.ContentView.frame = defaultFrame;
}
completion:^(BOOL finished){
}
];
}
As I understand the question, you are trying to shift the entire view when the UITextField becomes the first responder (i.e. adds the keyboard to the view)? If that is the case, I would add code in the UITextField delegate method:
#define VIEW_TAG 12345
#define kKeyboardOffsetY 80.0f
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// get a reference to the view you want to move when editing begins
// which can be done by setting the tag of the container view to VIEW_TAG
UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = CGRectMake(0.0f, -kKeyboardOffsetY, containerView.frame.size.width, containerView.frame.size.height);
}];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = CGRectMake(0.0f, self.view.frame.origin.y, containerView.frame.size.width, containerView.frame.size.height);
}];
}
add UITextFieldDelegate in .h file
and try the following code it will surely help you
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == username)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
if (textField == password)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField == username)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}
if (textField == password)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidShowNotification object:nil];
}
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float newVerticalPosition = -keyboardSize.height + 100;
[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
CGFloat kNavBarHeight = self.navigationController.navigationBar.frame.size.height;
[self moveFrameToVerticalPosition:kNavBarHeight forDuration:0.3f];
}
- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration
{
CGRect frame = self.view.frame;
frame.origin.y = position;
[UIView animateWithDuration:duration animations:^{
self.view.frame = frame;
}];
}
Take IBOutlet of your bottom constraint or top constraint of container view by ctrl+drag from respactive constraint.
If bottom constraint then increase the constant of constraint equal to keyboard height when keyboard appears and decrease same when keyboard disappear.
If using top constraint then decrease the constant equal to keyboard height when keyboard appears and increase same when keyboard disappear.
for example,
topConstraint.constant = topConstraint.constant + keyboardHeight;
Update :
According to your constraint you should take outlet of vertically center or center x and you should do something like,
self.verticallyCenter.constant = self.horizontalyCenter.constant - 100 //here 100 is keyboardheight for example and do it in viewdidload
By this your container view will goes up to 100 pixels.
when resign keyboard you can add 100 to it's constant to get initial position back.
Hope this will help :)
Seems like you have given correct constraints all you need to do now is to make a outlet of centre y constraint and change it's constant on keyboard show/hide events, Which you can grab with keyboardDidShow/keyboardWillShow and keyboardDidHide/keyboardWillHide notifications. that said you can also add cool animation effects for these changes. Try out let me know if you need example.
Edit:
add your viewcontroller as observer for two Notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Now suppose name of your constraint is "constraintForTopSpace",then add two methods for the notifications:
- (void)keyboardWasShown:(NSNotification *)notification {
// Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//Given size may not account for screen rotation
int height = MIN(keyboardSize.height,keyboardSize.width);
// Here you can set your constraint's constant to lift your container up.
[UIView animateWithDuration:0.5 animations:^{
[constraintForTopSpace setConstant:constraintForTopSpace.constant - height];
[self.view layoutIfNeeded];
}];
}
- (void)keyboardDidHide:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
int height = MIN(keyboardSize.height,keyboardSize.width);
// Here you can set your constraint's constant to move your container down.
[UIView animateWithDuration:0.5 animations:^{
[constraintForTopSpace setConstant:constraintForTopSpace.constant + height];
[self.view layoutIfNeeded];
}];
}
And you can also use UIKeyboardWillShowNotification/UIKeyboardWillHideNotification notification they will be triggered before the keyboard appears on screen, Its up to your requirements.
Here adding animation will give smooth look and feel. ;)
I need a way to be able to check if the keyboard when it shows up hides any element in the view. If so, i need the view to move up in a way that the element is shown but without the navigation bar moving.
Thanks in advance
#import "RequestViewController.h"
#define kOFFSET_FOR_KEYBOARD 80.0
#interface RequestViewController ()
#end
#implementation RequestViewController{
CGFloat keyboardHeight;
}
#synthesize descirptionTextView;
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view
descirptionTextView.text = #"Comment";
descirptionTextView.textColor = [UIColor lightGrayColor];
descirptionTextView.delegate = self;
descirptionTextView.layer.cornerRadius = 8;
// border
[descirptionTextView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[descirptionTextView.layer setBorderWidth:0.5f];
// drop shadow
[descirptionTextView.layer setShadowColor:[UIColor blackColor].CGColor];
[descirptionTextView.layer setShadowOpacity:0.8];
[descirptionTextView.layer setShadowRadius:3.0];
[descirptionTextView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
descirptionTextView.text = #"";
descirptionTextView.textColor = [UIColor blackColor];
return YES;
}
-(void) textViewDidChange:(UITextView *)textView
{
if(descirptionTextView.text.length == 0){
descirptionTextView.textColor = [UIColor lightGrayColor];
descirptionTextView.text = #"Comment";
[descirptionTextView resignFirstResponder];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Scrolling out of keyboard way
-(void)keyboardWillShow:(NSNotification *)nsNotification{
//first, get height of keyboard
NSDictionary *userInfo = [nsNotification userInfo];
CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardHeight = kbRect.size.height;
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
return;
}
-(void)keyboardWillHide{
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
return;
}
You can redraw your views in the keyboard delegate methods:
keyboardWillShow
keyboardWillHide
Declare a CGFloat property named keyboardHeight.
In your viewDidLoad method:
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
Keyboard methods:
-(void)keyboardWillShow:(NSNotification *)nsNotification{
//first, get height of keyboard
NSDictionary *userInfo = [nsNotification userInfo];
CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardHeight = kbRect.size.height;
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
return;
}
-(void)keyboardWillHide{
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
return;
}
You should also be able to substitute scrollView with self.view
When the keyboard appears, you are reducing the visible area by a considerable amount, so unless you have a layout that can be re-run with the smaller area, what you really desire is the ability to scroll.
In general, you'll want to choose where you scroll to based on which field is the firstResponder. This will guarantee that a user is never editing a field that they cannot see.
Revise your view hierarchy for this controller to be contained within a UIScrollView. Also, track which field is the firstResponder in an instance variable. Then, respond to keyboard notifications like this:
- (void)keyboardWillShow:(NSNotification*)notification
{
NSValue* keyboardFrameValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect kbRect = [self.view convertRect:keyboardFrameValue.CGRectValue fromView:nil];
CGRect overlap = CGRectIntersection(self.view.bounds, kbRect);
self.scroller.contentInset = UIEdgeInsetsMake(self.scroller.contentInset.top, 0, overlap.size.height, 0);
if (self.firstResponderView)
{
CGRect fieldRect = [self.scroller convertRect:self.firstResponderView.frame fromView:self.firstResponderView.superview];
[self.scroller scrollRectToVisible:fieldRect animated:YES];
}
}
Well, one can use a library like this one: https://github.com/michaeltyson/TPKeyboardAvoiding, or he can do it programmatically. This is how I proceeded:
//adding the notification about when keyboard appears and disappears when the view loads and remove them when the view will disappear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Then you add these selector methods
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
if(keyboardSize.height> (self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y)){
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y-keyboardSize.height - 10;
self.view.frame = f;
}];
}
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
I am very junior mobile programmer .I need to move up text views when keyboard appears.I follows this move-uiview-up-when-the-keyboard-appears-in-ios and it works well but I have a background image and I do not want to move up background image .so all textboxes are embed in UIView named as customView.I tried to move up customView instead of self.view .When I start enter in first textview, the customView moves up.But when I move to second textview,customview moves down to original position and textView become under the keyboard.customView need to stay move up the when i start enter in second textview .I really appreciate any help!.
#property (strong, nonatomic) IBOutlet UIView *customView;
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES; }
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES; }
- (void)keyboardDidShow:(NSNotification *)notification
{
//Assign new frame to your view
[self.customView setFrame:CGRectMake(0,50,320,460)];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.customView setFrame:CGRectMake(0,193,320,460)];
}
Add the observer in viewDidLoad for best approach.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification {
[UIView animateWithDuration:0.25 animations:^
{
CGRect newFrame = [customView frame];
newFrame.origin.y -= 50; // tweak here to adjust the moving position
[customView setFrame:newFrame];
}completion:^(BOOL finished)
{
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
[UIView animateWithDuration:0.25 animations:^
{
CGRect newFrame = [customView frame];
newFrame.origin.y += 50; // tweak here to adjust the moving position
[customView setFrame:newFrame];
}completion:^(BOOL finished)
{
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
// Add a scrollview on main view and add UITextField on that scrollview
-(void) viewDidLoad
{
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
myScrollView.contentSize = CGSizeMake(320, 500);
myScrollView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
[self.view addSubview:myScrollView];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
[myScrollView addSubview:myTextField];
myTextField.delegate = self;
}
// Set the scrollview content offset to make the myTextField move up
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES];
// here '80' can be any number which decide the height that textfiled should move
}
//To move the textfield to its original position
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[[myScrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}
Make your class implement the UITextFieldDelegate.
Put the following code in viewDidLoad.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
And define the following functions in your .m file.
- (void)keyboardWasShown:(NSNotification *)aNotification
{// scroll to the text view
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible.
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
self.scrollView.scrollEnabled = YES;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
// scroll back..
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
self.scrollView.scrollEnabled = NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
i am having 10 textfields over the scrollView. I want to scroll the view when keyboard Appears on Textfileds . Here i am using the below code to achieve this. But it is moving the screen up from the First Textfield my requirement is i want to move view up when keyboard come over the textfield only otherwise i don't want to move the view up.
Please help me.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[myScrollView setContentOffset:scrollPoint animated:YES];
}
Something like this maybe? (You may have to readapt it for landscape mode)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGFloat keyboardHeight = 216.0;
CGFloat viewHeight = myScrollView.frame.size.height;
BOOL covering = (textField.frame.origin.y - myScrollView.contentOffset.y) > (viewHeight - keyboardHeight);
if(covering) {
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[myScrollView setContentOffset:scrollPoint animated:YES];
}
}
There are ready solutions for this.
You could use TPKeyboardAvoiding to accomplish your mission.
Use keyboard notifications to be alerted when the keyboard is presented e.g.:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
(Also remove these in viewWillDissapear:
[[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
[[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
Then in the functions respectively:
- (void)keyboardWillShow:(NSNotification *) notification {
CGRect appFrame = YOUR_VIEWS_FRAME
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view.window convertRect:keyboardFrame toView:self.view];
UITextField *current = THE_CURRENT_RESPONDER
CGRect relativeFrame = [self.view convertRect:current.frame fromView:current.superview];
float difference = 0.0;
//Here we give it a 20 pixel padding between the top of the keyboard and the bottom of the text field.
if (CGRectIntersectsRect(relativeFrame, keyboardFrame))
difference = appFrame.size.height - keyboardFrame.size.height - 20 - relativeFrame.size.height - current.frame.origin.y;
difference = difference < 0 ? difference : 0;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.scrolLView setContentOffset:CGPointMake(0, difference);
} completion:^(BOOL finished) {
}];
}
And:
- (void)keyboardWillHide:(NSNotification *) notification {
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.scrolLView setContentOffset:CGPointZero];
} completion:^(BOOL finished) {
}];
}
In my app I have a UITextView that is in the bottom part of the screen. So what I do is the following code but the issue is, only sometimes if the text that I click on the UITextView is below the keyboard, it will scroll properly to above the keyboard.
Here is how I register the NSNotifications:
(In the ViewDidLoad)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification object:nil];
The methods:
-(void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary *info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [textView frame];
viewFrame.size.height -= keyboardSize.height;
textView.frame = viewFrame;
// Scroll the active text field into view.
//CGRect textFieldRect = [activeField frame];
[textView scrollRectToVisible:viewFrame animated:YES];
}
-(void)keyboardWasHidden:(NSNotification*)aNotification {
// Reset the height of the scroll view to its original value
CGRect viewFrame = [textView frame];
viewFrame.size.height += keyboardSize.height;
textView.frame = viewFrame;
}
How I unregister the NSNotifications:
In the ViewDidUnload:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
Does anyone see anything wrong?
Thanks!
here is the code I use to move the textview up so I can see it when the KB shows up. my view is in a scrollview already.
-(void)textViewDidBeginEditing:(UITextView *)textView { //Keyboard becomes visible
//perform actions.
NSLog(#"IN VIEW");
[self scrollTheViewToTop:self.scroller forTheTextView:textView];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[self.scroller setContentOffset:CGPointZero animated:YES];
}
- (void)scrollTheViewToTop:(UIScrollView *)scrollView forTheTextView:(UITextView *)textView {
//Scroll the ScrollView Up tp show the Continue button in the bottom is visible.
CGPoint pt;
CGRect rc = [textView bounds];
rc = [textView convertRect:rc toView:scrollView];
pt = rc.origin;
pt.x = 0;
pt.y -= 60;
[scrollView setContentOffset:pt animated:YES];
}