0

我正在开发一个 iphone 应用程序作为一个新手。我像动态创建 UItextFields

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

问题是,当我单击字段键盘时,会出现文本字段并且看不到文本字段。键盘出现在他们身上。键盘出现时如何更改文本字段的位置?

提前谢谢。

4

2 回答 2

1

在您的 Xib 中添加一个 UIScrollView 并建立 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 方法 fire 时,只需像这样更改内容偏移值。

- (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];    
}
于 2013-03-28T14:35:15.863 回答
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];

}

然后只需记住设置您的 textField 委托。

于 2013-03-28T15:49:15.663 回答