UITextView Not Scrolling after updating its frame - iphone

I have registered for notifications of Keyboard showing and Hiding.
The text View is scrollable when not clicked on it.. i.e. when it is not in editable mode..
When user click .. i update its frame to come up ..and after that it doesn't scroll.. after ending editing.. i change its position back its scrollable again..
Here is my code
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the scrollview
CGRect viewFrame = self.NoteTextView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.origin.y += (keyboardSize.height * 0.36);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:0.1];
[UIView commitAnimations];
[self.NoteTextView setContentSize:CGSizeMake(310,580)];
[self.NoteTextView setScrollEnabled:YES];
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
// This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown.
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.NoteTextView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.origin.y -= (keyboardSize.height * 0.36);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:0.3];
[self.NoteTextView setFrame:viewFrame];
[UIView commitAnimations];
[self.NoteTextView setContentSize:CGSizeMake(310, 580)];
[self.NoteTextView setScrollEnabled:YES];
keyboardIsShown = YES;
}
I have even enabled scrolling enabled and changed its content size in the methods.

It sound really weird.
My Suggestions:
1. Check that non of its superview's UserInteractioEnable property in the new frame isn't false
2. Try this code on new and clear project.

Related

Scroll view issues when key board moves up in UItextField / TextView iPhone

I have a scrollview on my view in that i have all the sub views as shown bellow
UIView
Scroll View
1.ImageView
2.Table view
3.Text View
√√ To move the scroll view when keyboard appears / dismiss I have implemented logic in textview delegate metods as follows √√
- (void)textViewDidBeginEditing:(UITextView *)textView;
{
//To move the view down
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y - 120), scrollView.frame.size.width, scrollView.frame.size.height);
[UIView commitAnimations];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
//To move the view down
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y + 120), scrollView.frame.size.width, scrollView.frame.size.height);
[UIView commitAnimations];
}
√√ This method helps alot to move the view up/down with respcet to keyboard,
But here is the problem of scrolling.
User can not scroll all the view in presensec of key board. The view scrolls up to some position as follows, we can't see the picture / first column of table.
If the user want to show the first column / profile pic in presence of keyboard doesn't possible. how to fix theis issue.
first set the scroll content like
scrollView.contentSize=CGSizeMake(320, 600);
and then animate there content when textView delegate method is called like
- (void)textViewDidBeginEditing:(UITextView *)textView;
{
[scrollView setContentOffset:CGPointMake(0, 120 ) animated:YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[scrollView setContentOffset:CGPointMake(0, 0 ) animated:YES];
}
First of all, you don't want to use the textViewDidBeginEditing: method to show/hide the keyboard. You should register for the appropriate notifications to do so. This could be placed in your viewDidLoad.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Then if the keyboard is shown, make sure to not move your entire scroll view but shrink it's size.
- (void)keyboardWillShow:(NSNotification *)notif
{
CGSize kbSize = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
double duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// Shrink the scroll view's content
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// Scroll the text field to be visible (use own animation with keyboard's duration)
CGRect textFieldRect = [_activeTextField convertRect:_activeTextField.bounds toView:self.scrollView];
[UIView animateWithDuration:duration animations:^{
[self.scrollView scrollRectToVisible:CGRectInset(textFieldRect, 0.0, -10.0) animated:NO];
}];
}
This code has some additional functionality because I also had to move the content of the scroll view, but works to get you started. Then when the keyboard hides...
- (void)keyboardWillHide:(NSNotification *)notif
{
double duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// Set content inset back to default with a nice little animation
[UIView animateWithDuration:duration animations:^{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}];
}
Apply content offset when keyboard is shown. And revert back when its hidden.
[scrollView setContentOffset:CGPointMake(0.0f, 120.0f) animated:YES];
After hiding the keyboard call.
[scrollView setContentOffset:CGPointZero animated:YES];
Just Change The ContentOffset of ScrollView No need to change the origin of ScrollView as you were doing in your code doing so scrollview will always scroll.
Doing so you need to set the ContentSize of upur ScrollViw atleast 550 just check what would be the Appropriate ContentSize. I have created a method in which you just need to call my method and pass required parameter appropriately
And One more Thing Set The ContentSIze Of UISCrollView More than the Height of It's SuperView at where you have created theScrollViewor inViewViewAppearmethod.
(CurrentView)
[contentScrollView setContentSize:CGSizeMake(320, 620)];
And Call This Method As you click on TextField or on the Screen(In Touch Ended when you need to readjust ContentOffset)
- (void)adjustContetOffset:(BOOL)yes withOffSet:(CGFloat)offSet
{
contentOffSetY = offSet ;
//By this you can track more suppose you have change the Orientation of Device then set Appropriate Offset.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
if(yes){
isScrolledUp = TRUE;
contentScrollView.contentOffset = CGPointMake(0, contentOffSetY);
}
else
{
isScrolledUp = FALSE;
//by this flag you can Track that OffSet of ScrollView has changed
contentScrollView.contentOffset = CGPointMake(0, contentOffSetY);
}
[UIView commitAnimations];
}
//this methods 'll call whenever you 'll touch the SCreen
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if(isScrolledUp){
[self adjustContetOffset:NO withOffSet: 0.0];
}
[searchTextField resignFirstResponder];
}
I hope it'll helpful to you.

NSNotificationCenter

Right now I'm trying to write a function to moving up frame when keyboard appears on the screen.
I started to use NSNNotificationCenter. My code is working but not correctly. When keyboard appears my formView is moving up but when I start to edit next textField in formView, formView is moving up again. What's wrong with my code?
Thanks.
- (void)keyboardWillShow:(NSNotification *) aNotification {
NSDictionary *userInfo = [aNotification userInfo];
CGRect frame = self.formView.frame;
frame.origin.y -= 170;
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
formView.frame = frame;
[UIView commitAnimations];
}
You should add again your 170 pixels (or whatever you calculate as suggested by Mike) to the origin.y of your view when the keyboard is disappearing. When you click on another text field, technically the current keyboard will disappear (your view does not react in any way) and a new keyboard will appear (your keyboardWillShow will be called again and you shift your view again up by 170 px).

IPhone Keyboard hides UISeachBar

I have a search bar at the bottom of a view. The issue is whenever I type something in the keyboard, the search bar still remains at the bottom and I cannot view what I am typing. I would like to dynamically move it up whenever the keyboards pops up and then take back its original position after the keyboard disappears. And unfortunately the search bar has to be in the bottom for my app and kind of stuck in this.
Is there any way around to move the search bar up only when the keyboard appears? Any code snippets would be really helpful.
Your best bet is probably to use the –searchBarShouldBeginEditing: in the UISearchBarDelegate protocol.
It would look something like this :
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
CGRect newFrame = //some rect
mySearchBar.frame = newFrame;
return YES;//this is important!
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
CGRect originalFrame = //the original frame
mySearchBar.frame = originalFrame;
return YES;
}
EDIT: One of the other answers suggests using the UIKeyboard notifications, but that can be confusing. It does, however, give the advantage of working for every time the keyboard appears, rather than just when the UISearchBar is the first responder.
EDIT 2:
Mayur Joshi suggests using animation, which can be done like so:
[UIView animateWithDuration:duration
animations:^{
//what you want to animate (in this case, the search bar's frame)
}
completion:^(BOOL finished){
//what to do when the animation finishes
}];
EDIT 3:
If you don't want to obscure the view behind the search bar, you will have to shrink its height whenever the search bar moves up. It can go in the same place as the code to animate your search bar.
Hi, try this
sBar is UISearchBar object.
- (void)keyboardWillShow:(NSNotification *)aNotification
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = sBar.frame;
NSLog(#"%f",frame.size.height);
NSLog(#"%f",frame.origin.y);
frame.origin.y=frame.origin.y- keyboardRect.size.height;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
sBar.frame = frame;
NSLog(#"%f",frame.size.height);
NSLog(#"%f",frame.origin.y);
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
// the keyboard is hiding reset the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = sBar.frame;
NSLog(#"%f",frame.size.height);
NSLog(#"%f",frame.origin.y);
frame.origin.y=frame.origin.y+ keyboardRect.size.height;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
sBar.frame = frame;
NSLog(#"%f",frame.size.height);
NSLog(#"%f",frame.origin.y);
[UIView commitAnimations];
}
You will have to use a UIScrollView and keep this search bar in that scroll View. Now, when you start editing the Search Bar, that is,
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
set the scrollRectToVisible method for the UIScrollView so as to make your SearchBar visible like this....
[yourScrollView scrollRectToVisible:CGRectMake(0, 300, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];
And when you have written the text in the search bar, i.e
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1
set the Scroll view scrollRectToVisible to its original size.
[yourScrollView scrollRectToVisible:CGRectMake(0, 0, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];

Handling keyboard show/hide & orientation changes

I use the following method to resize the view after keyboard show/hide:
- (void)moveViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {
NSDictionary* userInfo = [aNotification userInfo];
// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
// Animate up or down
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
newFrame.size.height += (up? -1 : 1) * keyboardFrame.size.height;
self.view.frame = newFrame;
keyboardUp = up;
[UIView commitAnimations];
}
This works very good, until the screen orientation changes. What happens then is that the method resizes the view correctly, but after this method returns - something else resizes the view again to the maximum height of the screen.
Any ideas?
First you'll need to figure out what is resizing the view again. I would recommend putting a breakpoint into the above method so when it gets called that 2nd, unwanted time, you can look at the method call stack and see what is causing the 2nd call. Then you'll be able to stop it from happening, or change your code so that it doesn't cause a problem.

Hiding keyboard with UIScrollView without glitches

I have multiple editable textfiels and some of them are covered with keyboard. So I used UIScrollView and it works quite nice.
Problem is when I want to hide keyboard. If I was scrolled down, after the keyboard hides, everything jumps up as it was at beginning (without keyboard). I want to tween this part as the keyboard is hiding.
So far I got this code (2 methods for keyboard events):
-(void)keyboardWillShow:(NSNotification *)notif{
if(keyboardVisible)
return;
keyboardVisible = YES;
NSDictionary* info = [notif userInfo];
NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect viewFrame = self.view.frame;
viewFrame.size.height -= keyboardSize.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[scrollView setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notif{
if(!keyboardVisible)
return;
keyboardVisible = NO;
NSDictionary* info = [notif userInfo];
NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect viewFrame = self.view.frame;
viewFrame.size.height += keyboardSize.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[scrollView setFrame:viewFrame];
[UIView commitAnimations];
}
It works pretty well with hiding keyboard but unfortunately it doesn't work when user switches from one text field to another. It will fire keyboardWillHide and keyboardWillShow events, one right after another. This will result in two animations, second one interrupting the first one. It doesn't look good.
Problem is with keyboardWillHide firing even when keyboard will not hide. At that point I don't know if keyboard will be shown again or not.
I also tried it with UIScrollView scrollRectToVisible and setContentOffset methods.. but they resulted in glitches when keyboard was hiding.
use this method to handle multiple text field and keyboard
-(void)scrollViewToCenterOfScreen:(UIView *)theView
{
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard  
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
call it in textfield delegate=
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self scrollViewToCenterOfScreen:textField];
}
and set scroll view frame in the below textfield delegate=
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
return YES;
}
Why not use boolean values to indicate whether it is an appearance or just changing?