动态创建uitextfield的屏幕位置

时间:2013-03-28 13:51:19

标签: objective-c uitextfield

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

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

问题是,当我点击一个字段键盘出现时,无法看到文本字段。键盘出现在他们身上。如何在键盘显示时更改文本字段的位置?

提前完成。

2 个答案:

答案 0 :(得分: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];    
}

答案 1 :(得分: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委托。