2013-02-25 55 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

相關問題