选择UITextField时向上移动UIView

时间:2013-02-13 16:52:18

标签: ios uiview uitextfield

我正在尝试以编程方式创建一些文本字段。它们出现在屏幕的底部,选择后键盘将覆盖它们。我需要在选择文本字段时向上移动视图。

以下是我的视图代码:

部首:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> {
    IBOutlet UITextField *usernameField;
}

Imp视图文件:

// Create sub view for Logo
    UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)];
    [logoView setBackgroundColor:[UIColor blueColor]];
    logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

    // Create sub view for fields
    UIView *fieldView = [[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 200)];
    [fieldView setBackgroundColor:[UIColor greenColor]];
    fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;

    // Setting up the text fields
    // Username field
    usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
    usernameField.borderStyle = UITextBorderStyleRoundedRect;
    usernameField.textColor = [UIColor blackColor];
    usernameField.font = [UIFont systemFontOfSize:17.0];
    usernameField.placeholder = @"Username";
    usernameField.backgroundColor = [UIColor whiteColor];
    usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
    usernameField.keyboardType = UIKeyboardTypeDefault;
    usernameField.returnKeyType = UIReturnKeySearch;
    usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    usernameField.delegate = self;

    // Password field
    UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(10, (usernameField.bounds.size.height + 30), 300, 40)];
    passwordField.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
    passwordField.borderStyle = UITextBorderStyleRoundedRect;
    passwordField.textColor = [UIColor blackColor];
    passwordField.font = [UIFont systemFontOfSize:17.0];
    passwordField.placeholder = @"Password";
    passwordField.backgroundColor = [UIColor whiteColor];
    passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
    passwordField.keyboardType = UIKeyboardTypeDefault;
    passwordField.returnKeyType = UIReturnKeySearch;
    passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordField.delegate = self;

    // Add fields/button to fieldView
    [fieldView addSubview:usernameField];
    [fieldView addSubview:passwordField];

    // Add subviews to main view
    [self.view addSubview:logoView];
    [self.view addSubview:fieldView];

我添加了此方法来移动视图,但无法正确计算出最后一部分:

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textFieldDidBeginEditing");
    //If we begin editing on the text field we need to move it up to make sure we can still
    //see it when the keyboard is visible.
    //I am adding an animation to make this look better
    [UIView beginAnimations:@"Animate Text Field Up" context:nil];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    usernameField.frame = CGRectMake(usernameField.frame.origin.x,
                                        -200,
                                        usernameField.frame.size.width,
                                        usernameField.frame.size.height);

    [UIView commitAnimations];

}

我实际需要的是完全移动视图,或者可能将其更改为上方的可滚动区域。虽然我在触摸endEditing后实现了另一种方法。

似乎我移动一个字段我必须完成代码才能移动所有...我怎样才能移动视图(完整视图)?

还有什么方法需要在方法textFieldDidEndEditing | textFieldShouldEndEditing中返回正常状态?

4 个答案:

答案 0 :(得分:0)

您正在我朋友的方法中访问本地变量usernameField。那是编译器错误。

答案 1 :(得分:0)

如果我的理解是正确的 - 你实际上想要转移“fieldView” - 因为两个UITextFields是这个视图的子视图。

您抛出该错误的原因是因为该方法无法访问该变量 - 您需要在头文件中声明一个实例变量。

答案 2 :(得分:0)

在键盘选中后进行动画制作 - 改为为整个视图设置动画。

首先声明UIView变量以使其可访问:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> 
{ 
IBOutlet UITextField *usernameField; 
UIView *fieldView; 
}

然后在您的实现中创建此视图

 // Create sub view for Logo
UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)];
[logoView setBackgroundColor:[UIColor blueColor]];
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

// Create sub view for fields
fieldView = [[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 200)];
[fieldView setBackgroundColor:[UIColor greenColor]];
fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;

// Setting up the text fields
// Username field
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
usernameField.borderStyle = UITextBorderStyleRoundedRect;
usernameField.textColor = [UIColor blackColor];
usernameField.font = [UIFont systemFontOfSize:17.0];
usernameField.placeholder = @"Username";
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.returnKeyType = UIReturnKeySearch;
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.delegate = self;

// Password field
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(10, (usernameField.bounds.size.height + 30), 300, 40)];
passwordField.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
passwordField.borderStyle = UITextBorderStyleRoundedRect;
passwordField.textColor = [UIColor blackColor];
passwordField.font = [UIFont systemFontOfSize:17.0];
passwordField.placeholder = @"Password";
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordField.keyboardType = UIKeyboardTypeDefault;
passwordField.returnKeyType = UIReturnKeySearch;
passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordField.delegate = self;

// Add fields/button to fieldView
[fieldView addSubview:usernameField];
[fieldView addSubview:passwordField];

// Add subviews to main view
[self.view addSubview:logoView];
[self.view addSubview:fieldView];

然后为fieldView设置动画:

- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//I am adding an animation to make this look better
[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];

fieldView.frame = CGRectMake(fieldView.frame.origin.x,
                                    -200,
                                    fieldView.frame.size.width,
                                    fieldView.frame.size.height);

[UIView commitAnimations];

}

由于所有文本字段都是fieldView的子视图,因此它们将使用它进行动画处理。应该排序!

答案 3 :(得分:0)

只需在.h和.m文件中使用以下代码,它就可以正常使用。

-------的.h -----

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (IBAction) textFieldReturn:(id)textField
{
    [textField resignFirstResponder];
}

我已经获得了solution here