Textfield inside the scrollView iPhone - iphone

I am new to iPhone, I have 10 textfields in my application which are in a scrollview.
What i need is when user touches on a textfield,scrollview should scroll in such a way, so that textfield should not be behind the keyboard.
Help me.
Thanks for your kind help to me.

override the textfield delegate methods like this
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self scrollViewToCenterOfScreen:textField];
}
//This method moves the current selected text field to the visible zone
-(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];
}
And when you want to dismiss the keyboard override this delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
return YES;
}

Related

Back ground resizing on keyboard visiblity

I'm using scroll view in my application ,since when i click on dob text-field the datepicker view is showing as a pop up ,on further when i click in continuous text field the view is being like in the image,Here my code,
For date picker visibility.
UIDatePicker pop up after UIButton is pressed
For keyboard orientation
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[dob resignFirstResponder];
if (txt1.textColor == [UIColor lightGrayColor]) {
txt1.text = #"";
txt1.textColor = [UIColor blackColor];
}
if ([textField isEqual:dob])
{
[self but];
[dob resignFirstResponder];
//return NO;
}
//[self animateTextField:textField up:YES];
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.1 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//[self animateTextField:textField up:NO];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];
}
Can any one help me to clear.
You need to resize your scrollview when the key board appears.
this can be done by adding:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(keyboardShown) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:#selector(keyboardHidden) name:UIKeyboardWillHideNotification object:nil];
in the viewDidLoad.
Then define the methods keyboardShown and keyboardHidden in your implementation file.
These methods will be called automatically when keyboard appears and disappears.
In these methods resize your background view accordingly. since you are using scroll view please mind the scrollView's contectView size also. as its different from the view's frame size.
For the basic problem of having a scroll view and needing to move its contents when the keyboard appears, I have found this open source scroll view class by Michael Tyson to be very useful.
Basically, you just add the TPKeyboardAvoidingScrollView class to your project, and use it where you would normally use a UIScrollView. The child views that you add to the TPKeyboardAvoidingScrollView can be UITextField objects, if you like. When you tap those fields to begin editing, and the keyboard appears, the TPKeyboardAvoidingScrollView container will choose an appropriate scroll position so that the field that's being edited is not hidden by the keyboard, and you won't see black bars like you have in your screen shot.
I think i get the problem.
jst for example, in ur register page, many textfields and button for DOB.
So, basically when any textfield editing and after that instead of return keyboard, user press button of DOB.
so u have to first resignFirstRespoder ur all textfields.
Problem is when u startEditing frame set to top and endEditing set to bottom but when u click to button endEditing not called so frame not set to bottom. And again u startEditing frame again set to top so problem aries.
I hope this help...

UIScrollView and multiple UITextField

I have a little problem with uitextfield & uiscrollview, the problem is that when I click on uitextfield the keyboard hide the field, for solve this I use:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGRect viewFrame = self.scroll.frame;
viewFrame.size.height -= 186;
[self.scroll setFrame:viewFrame];
return YES;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
[self inserisciField];
[textField resignFirstResponder];
return YES;
}
and all work, the problem is with multiple field, with a field selected if I change the field without click return the scroll view has a wrong size/positon but if I click return, then I Select a field, press return, change field, ecc...all work.
I think that there is this problem because If I change field the scroll dimension was changed two time (the first time view frame.size.height -= 186, and another time with -=186) then I try to put this method
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
return YES;
}
where I set the default size of frame, but nothing.
use this -
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint point = CGPointMake(scroll.frame.origin.x, scroll.frame.origin.y + 186);
[self.scroll setContentOffset:point];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
CGPoint point = CGPointMake(0, 0);
[self.scroll setContentOffset:point];
}
You can use the contentoffset property of scrollview when u touch or edit a textfield as it is the best solution for your problem and touch and edit are the same process - only when you touch the field, it becomes editable.

UIScrollView scrolling issue

I have a scrollview which works perfectly until I dismiss the keyboard. After that it scrolls but not all the way down anymore... I am a bit puzzled.
in viewDidLoad I save the frame so I can reset it later
frame = self.scrollView.frame;
NSLog(#"Height beginning: %f",self.scrollView.frame.size.height);
offset = self.scrollView.contentOffset;
For debugging I checked the scrollview's height which is 629
in my keyboardDidHide method I set the old frame back
self.scrollView.frame = frame;
NSLog(#"Height end: %f",self.scrollView.frame.size.height);
// Reset the scrollview to previous location
self.scrollView.contentOffset = offset;
The debugging output is 629 as well which means that the scrollview's height has been set to the old value. It does scroll but when I let go it bounces all the way back to the beginning...
EDIT:
When using 480 as height it is not filling the whole screen because of iphone 4
Some code
-(void) keyboardDidShow: (NSNotification *)notif
{
NSLog(#"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(#"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
CGRect keyboardEndFrame;
[[notif.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = frame;
viewFrame.size.height = 200;
//viewFrame.size.height -= keyboardEndFrame.size.height;
self.scrollView.frame = viewFrame;
offset = self.scrollView.contentOffset;
CGRect textFieldRect;
if(!activeField)
textFieldRect = comment.frame;
else
textFieldRect = activeField.frame;
textFieldRect.origin.y += 10;
[self.scrollView scrollRectToVisible:textFieldRect animated:YES];
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif
{
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(#"Keyboard is already hidden. Ignore notification.");
return;
}
//viewFrame.size.height -= keyboardEndFrame.size.height;
self.scrollView.frame = frame;
[self.scrollView setContentSize:CGSizeMake(320, 700)];
// Reset the scrollview to previous location
self.scrollView.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}

How to hide keybord while typing in iphone app

I have 2 textview one is on top and other is at bottom. When I am trying to write something on bottom textview keyboard is coming on screen and I am not able to see what I am typing.
How to make keyboard invisible or placed on perfect place while typing below textview?
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == bottomTextView) {
CGRect frame = self.view.frame;
frame.origin.y -= 250;
self.view.frame = frame;
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == bottomTextView) {
CGRect frame = self.view.frame;
frame.origin.y += 250;
self.view.frame = frame;
}
}
You can modify this 250 value.
The Answer to your problem lies here buddy..
Sliding UITextFeilds when keyboard appears
Its Best solution available.
Write This Delegate Methods For TextField
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == txtUserName) {
[txtUserName becomeFirstResponder];
[scrollView setContentOffset:CGPointMake(0,45) animated:YES];
} else if (textField == txtPassword) {
[txtPassword becomeFirstResponder];
[scrollView setContentOffset:CGPointMake(0,105) animated:YES];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0,0) animated:YES];
return YES;
}

detect when uiscrollview frame touch the main screen bounds

how can i detect when a UIScrollViewframe intersect with the main screen (when scrolling) in both direction up and down? i was trying to do it like so :
- (void)detectScreenBoundsIntersect{
if (CGRectIntersectsRect([myScrollView frame], [[UIScreen mainScreen] bounds])) {
NSLog(#"COLLISION");
}
}
But this doesn't seem to work! Thanks.
Adopt the UIScrollViewDelegate protocol in the view controller and set the scroll view's delegate to the controller. Adopt the scrollViewDidScroll: method.
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
if ( [self hasReachedAVerticalEdge] ) {
NSLog(#"At World's End");
}
}
- (BOOL) hasReachedAVerticalEdge {
CGPoint offset = myScrollView.contentOffSet;
CGSize contentSize = myScrollView.contentSize;
CGFloat height = myScrollView.frame.size.height;
CGFloat width = myScrollView.frame.size.width;
if ( offset.y == 0 ||
(offset.y + height) == contentSize.height ) {
return YES;
}
return NO;
}
Is this what you are looking for?
Checking if an image view falls in the visible portion of the scroll view.
- (BOOL)isContentFrameVisible:(CGRect)aFrame {
CGRect visibleRect = CGRectZero;
visibleRect.origin = myScrollView.offset;
visibleRect.size = myScrollView.frame.size;
if ( CGRectIntersectsRect(visibleRect, aFrame) ) {
return YES;
}
}