2

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

这是我的视图代码:

标题:

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

小鬼查看文件:

// 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

4 回答 4

0

您正在使用我朋友的方法访问局部变量usernameField。那是编译器错误。

于 2013-02-13T16:55:13.560 回答
0

如果我的理解是正确的——你实际上想改变“fieldView”——因为这两个 UITextFields 是这个视图的子视图。

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

于 2013-02-13T17:02:16.943 回答
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 的子视图 - 它们将使用它进行动画处理。那应该排序!

于 2013-02-18T21:53:26.167 回答
0

只需在您的 .h 和 .m 文件中使用以下代码,它对我来说效果很好。

- - - -。H - - -

CGFloat animatedDistance;

-----.m----------

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

我在这里找到了解决方案

于 2013-11-15T11:20:02.980 回答