0

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. enter image description here

Community
  • 1
  • 1
iosdev
  • 277
  • 8
  • 19

3 Answers3

0

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.

Apple_iOS0304
  • 1,092
  • 1
  • 9
  • 19
  • By the way the keyboard size is 320 x 216 pts in iphone. just resize your view by subtracting 216 from its height and you might achieve what you want!! – Apple_iOS0304 Sep 21 '12 at 05:12
  • its already there in the date picker view,just see in that question link – iosdev Sep 21 '12 at 05:27
  • I just edited your link for the question post. i believe you first need to understand the code really well, spcly the ones that others give to you. It will help you solve such problems. As per my understanding your view is pushed up when the keyboard appears, after your datepicker was selected. Also make sure that when you select a textfield you have an IBAction attached to your textfields that resets your background's frame to fullscreen. Do this on the textfields touchDown. Then try and resize your view as per the keyboard view. – Apple_iOS0304 Sep 21 '12 at 06:19
0

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
0

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...

iBhavik
  • 663
  • 11
  • 28