2013-02-25 51 views
0

我有一个注册表单,定义如下,我希望在滚动视图中具有此表单,以便用户在底部滚动时选择文本框。为示例表单添加滚动视图ehich包含几个文本框(iphone)

事情是,注册表单不覆盖整个屏幕,并在后台我仍然有主视图,我希望textfields滚动窗体内,而不会改变窗体的窗口的位置(我不希望窗体视图出现并覆盖背景)。

- (UIView *)Form 
{ 
    if (!_Form) { 

     CGRect frame = CGRectMake(0.0, Height, Width, 310.0); 
     UIView *container = [[UIView alloc] initWithFrame:frame]; 

     CGFloat y = 15.0; 
     frame = CGRectMake(15.0, y, width, height); 
     UITextField *field = [[UITextField alloc] initWithFrame:frame]; 
     [ 
     field.placeholder = @"text1*"; 


     CGFloat spacing = 8.0; 
     y = frame.origin.y + kDefaultTextFieldHeight + spacing; 
     frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight); 
     field = [[UITextField alloc] initWithFrame:frame]; 

     field.placeholder = @"Password*"; 


     frame.size.height = 200.0; 

     y = frame.origin.y + kDefaultTextFieldHeight + spacing; 
     frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight); 
     field = [[UITextField alloc] initWithFrame:frame]; 

     field.placeholder = @"Confirm Password*"; 



     y = frame.origin.y + kDefaultTextFieldHeight + 16.0; 
     CGFloat w = (kDeviceWidth - 2 * 15.0)/2; 
     frame = CGRectMake(15.0, y, w - 2.0, height); 
     field = [[UITextField alloc] initWithFrame:frame]; 

     field.placeholder = @"First Name*"; 


     frame = CGRectMake(15.0 + w + 2.0, y, w - 2.0, height); 
     field = [[UITextField alloc] initWithFrame:frame]; 

     field.placeholder = @"Last Name*"; 
     [container addSubview:field]; 

     y = frame.origin.y + kDefaultTextFieldHeight + spacing; 
     frame = CGRectMake(15.0, y, w, kDefaultTextFieldHeight); 
     field = [[UITextField alloc] initWithFrame:frame]; 

     field.placeholder = @"Birthday"; 



     y = frame.origin.y + kDefaultTextFieldHeight + 20.0; 
     frame = CGRectMake((container.frame.size.width - 192.0)/2, y, 192.0, 34.0); 



     y = frame.origin.y + kDefaultTextFieldHeight + 8.0; 
     frame = CGRectMake((container.frame.size.width - 192.0)/2, y, 192.0, 34.0); 



     _Form = container; 
    } 
    return _Form; 
} 

谢谢!

回答

0

尝试以下按照满足您的要求更改代码:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 


- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    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.5 * 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 
{ 
    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 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 
+0

但这方法只抬起形式进行滚动它也将覆盖整个屏幕。我希望表格被固定在其位置并在里面滚动。 – 2013-02-25 11:52:39

+0

Hi @johnMacL,将您创建的所有字段添加为UIScrollview的子视图并尝试。 – 2013-02-25 12:18:35

+0

你能帮我解码吗?谢谢 – 2013-02-25 14:44:48

相关问题