2013-03-28 91 views
0

我正在開發一個iPhone應用程序作爲新手。我動態創建像動態創建uitextfield的屏幕位置

for (int i = 0; i <= currentAnswerLen; i++) { 
    [textField4 setTag:i]; 
    textField4 = [[UITextField alloc] initWithFrame:CGRectMake((50 + (i * 40)), 180, 30, 35)]; 
etc....... 
} 

的問題是,當我點擊一個領域鍵盤出現,文本框無法看到UItextFields。鍵盤出現在它們上面。如何在鍵盤顯示時更改文本字段的位置?

thanx提前。

回答

0

在你的界面添加此變量

@interface example : UIViewController 
{ 
    CGFloat animatedDistance; 

} 

在您的實現類添加此

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]; 

} 

然後只記得設置y我們的textField委託。

+0

thanx男人的魅力! – koray

1

將UIScrollView添加到您的Xib並創建IBOutlet連接,即添加您動態創建的所有標籤。 我在你的代碼中做了一些修改。它的工作正常。

-(void)setAnswerField 
{ 
    int len = 10; 
    int commonSpace = 20; 
    for (int i=0;i<=len;i++) 
    { 
     if(i != 0) 
      commonSpace += 40; 
     textF=[[UITextField alloc] initWithFrame:CGRectMake(((i * 60))+commonSpace,180,60,35)]; 
     [textF setTag:i]; 
     [textF setDelegate:self]; 
     [textF setReturnKeyType:UIReturnKeyDone]; 
     [textF setBackgroundColor:[UIColor grayColor]]; 
     [textF addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
     textF.clearButtonMode=UITextFieldViewModeWhileEditing; 
     [textF addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit]; 
     [_textfieldsScrollView addSubview:textF]; 
    } 

    _textfieldsScrollView.contentSize=CGSizeMake(1000, 500); 
} 

在點擊文本字段textViewDidBeginEditing方法時,只需將內容從設置值更改爲這樣。

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 

     [_textfieldsScrollView setContentSize:CGSizeMake(320, 400)]; 
     [_textfieldsScrollView setContentOffset:CGPointMake(0, 100) animated:YES]; 

} 

之後,恢復到原來的位置。

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    //Back to normal state. 
    [_textfieldsScrollView setContentOffset:CGPointMake(0, 0) animated:YES];  
} 
+0

其實你的答案是有道理的,但我想我失去了一些東西;當動態創建爲[editScrollView addSubview:textField]時,我添加了textfield滾動視圖;但是當應用程序啓動時,textfields不會再出現在屏幕上! – koray

+0

發佈您的整個代碼以動態添加測試字段。 – Ganapathy

+0

- (void)setAnswerField {int i = 0; i <= len; i ++){textF setTag:i]; [textF setDelegate:self]; [textF setReturnKeyType:UIReturnKeyDone]; [textF addTarget:self action:@selector(textFieldFinished:)forControlEvents:UIControlEventEditingDidEndOnExit]; textF = [[UITextField alloc] initWithFrame:CGRectMake((50 +(i * 40)),180,30,35)]; textF.clearButtonMode = UITextFieldViewModeWhileEditing; [textF addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit]; [_textfieldsScrollView addSubview:textF]; }} – koray