Ios 选择UITextField时向上移动UIView

Ios 选择UITextField时向上移动UIView,ios,uiview,uitextfield,Ios,Uiview,Uitextfield,我试图通过编程创建一些文本字段。它们出现在屏幕底部,选中时键盘会覆盖它们。选择文本字段时,我需要向上移动视图 以下是我的视图代码: 标题: @interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> { IBOutlet UITextField *usernameField; } 我添加了此方法来移动视图,但无法正确计算出最

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

以下是我的视图代码:

标题:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> {
    IBOutlet UITextField *usernameField;
}
我添加了此方法来移动视图,但无法正确计算出最后一部分:

- (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之后实现了另一种方法

似乎如果我移动一个字段,我必须完成代码才能移动所有字段。。。如何移动视图(完整视图)


另外,在方法textfielddendediting | textfield shouldediting中,视图返回正常状态需要什么?

您正在访问方法my friend中的局部变量
usernameField
。这就是编译器的错误。

如果我的理解是正确的,您实际上想要移动“fieldView”,因为两个UITextFields是此视图的子视图


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

当您在选中键盘后设置动画时,请改为设置整个视图的动画

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

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> 
{ 
IBOutlet UITextField *usernameField; 
UIView *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的子视图,因此它们将使用它设置动画。那就对了

只需在您的.h和.m文件中使用以下代码,对我来说就可以了

-------h-----

--------m----------

静态常量CGFloat键盘\u动画\u持续时间=0.3;
静态常数CGFloat最小滚动分数=0.2;
静态常数CGFloat最大滚动分数=0.8;
静态常数CGFloat纵向键盘高度=216;
静态常数CGFloat横向键盘高度=162;
-(无效)textFieldDidBeginEditing:(UITextField*)textField
{
CGRect textFieldRect=
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect=
[self.view.window convertRect:self.view.fromView:self.view];
CGFloat midline=textFieldRect.origin.y+0.5*textFieldRect.size.height;
CGFloat分子=
中线-viewRect.origin.y
-最小滚动分数*viewRect.size.height;
浮点分母=
(最大滚动分数-最小滚动分数)
*viewRect.size.height;
CGFloat heightFraction=分子/分母;
如果(高度分数<0.0)
{
高度分数=0.0;
}
否则如果(高度分数>1.0)
{
高度分数=1.0;
}
界面定向=
[[UIApplication sharedApplication]statusBarOrientation];
如果(方向==UIInterfaceOrientationParative||
方向==UIInterfaceOrientationGraphitalUpsideDown)
{
动画距离=地板(纵向\键盘\高度*高度分数);
}
其他的
{
//动画距离=地板(横向键盘高度*高度分数);
}
CGRect viewFrame=self.view.frame;
viewFrame.origin.y-=动画距离;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}
-(void)textfielddidediting:(UITextField*)textField
{
CGRect viewFrame=self.view.frame;
viewFrame.origin.y+=动画距离;
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationBeginsFromCurrentState:是];
[UIView设置动画持续时间:键盘动画持续时间];
[self.view setFrame:viewFrame];
[UIView委员会];
}
-(BOOL)textField应返回:(UITextField*)textField
{
[textField resignFirstResponder];
返回YES;
}
-(iAction)textField返回:(id)textField
{
[textField resignFirstResponder];
}

我已经得到了

好的,我已经更新了代码,我将更新问题。我现在也可以移动字段,但实际上我希望整个视图,而不是仅更改文本字段的单个框架…您可以逐个或整个视图本身移动所有视图。你有为用户名字段进行转换的代码,为什么不将其用于其他视图?我不确定如何。。。我如何移动整个视图,包括子视图才是真正的问题。移动usernamefield和passwordfield是否足够?如果是这样的话,您可以向上移动
fieldView
,就像您对usernamefield所做的那样。我想移动整个视图,但不知道如何。。。这是我正在寻找的正确答案。我已经用正确的代码更新了问题,但仍然不确定如何移动整个视图。您可以移动fieldView(其中包含您的用户名字段和密码字段)或整个视图(self.view),我不确定如何。。。如何移动整个视图(包括子视图)是实际问题。如果移动主视图-所有子视图都将随之移动。我要移动整个视图(主视图)。到目前为止,我还没有一个关于如何做到这一点的答案…谢谢,但然后徽标视图也不会移动,所以如果视图没有覆盖徽标视图,您将看到重叠。是否有任何方法可以移动整个视图。我是否应该创建另一个变量作为整个视图并更新origin.y呢?哦,我的错误-很容易修复。在textFieldDidBeginEditing方法中使用self.view.frame而不是fieldView.frame,这将移动整个视图。
 // 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];
- (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];
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];
}