简体   繁体   English

编辑时按下主屏幕按钮时,xCode TextField键盘视图滚动问题

[英]xCode TextField keyboard view scroll issue when home button is pressed while editing

I currently use this method to scroll the view up and down when I have multiple text fields on the screen and don't want the keyboard hiding the text field. 当我在屏幕上有多个文本字段并且不希望键盘隐藏文本字段时,我目前使用此方法在视图上上下滚动。 I found this code online and it works very well for the most part. 我在网上找到了此代码,并且在大多数情况下效果很好。

In the interface file 在界面文件中

@interface ViewController : UIViewController<UITextFieldDelegate>
{

    IBOutlet UITextField *textField1;
    IBOutlet UITextField *textField2;
    IBOutlet UITextField *textField3;

    //Float
    CGFloat animatedDistance;

}

in the implementation 在实施中

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;

Then I use these methods to make the view scroll up and down when text field is clicked 然后,当单击文本字段时,我使用这些方法使视图上下滚动

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

In this example, I have 3 text fields, positioned at top of the screen, middle of the screen and bottom of the screen. 在此示例中,我有3个文本字段,分别位于屏幕顶部,屏幕中间和屏幕底部。 I also have this tool bar added to each text field with next previous and done buttons in it. 我还将此工具栏添加到每个文本字段,并在其中包含下一个上一个和完成的按钮。 I am sure you can guess what they do. 我相信您可以猜到他们在做什么。

This code works well except for one issue. 除了一个问题外,此代码效果很好。 If I am editing a text field, and I click on a text field in the middle of the screen, the view scrolls up so the text field isn't hidden, which is good. 如果我正在编辑文本字段,然后单击屏幕中间的文本字段,则视图会向上滚动,因此不会隐藏该文本字段,这很好。 However if I then click the home button while in the middle of editing, then access the application again, when the view is opened again, the scroll is reset back to way it normally is, but the textfield is still being edited, so when I do the click the done button, the view acts is if it is still scrolled up, so it scrolls back down again, which causes the view to scroll off the bottom of the screen 但是,如果我在编辑过程中单击“主页”按钮,然后再次访问该应用程序,则在再次打开视图时,滚动将重置为正常状态,但是文本字段仍在编辑中,所以当我单击“完成”按钮,该视图的作用是是否仍在向上滚动,因此它再次向下滚动,这导致该视图从屏幕底部滑出

I have also tried using notifications for when keyboard is shown or hidden to scroll the view, but same problem happens. 我也尝试使用通知来显示或隐藏键盘以滚动视图,但是同样的问题也会发生。

Has anyone had this issue before? 有人遇到过这个问题吗? And if so, how did they resolve it. 如果是这样,他们是如何解决的。

Thanks in advance 提前致谢

Even i got this issue, and fixed like below. 甚至我也遇到了这个问题,并像下面这样修复。

You need to add an observer for UIApplicationWillResignActiveNotification in your ViewController where textFields are present. 您需要在存在UIApplicationWillResignActiveNotification的ViewController中为UIApplicationWillResignActiveNotification添加观察者。

And for that action resign all the keyboards ( resignFirstResponder ). 对于该操作,请退出所有键盘( resignFirstResponder )。

This makes your view in right position before app entering background. 这样可以使您的视图在应用进入背景之前处于正确的位置。

And make sure add & remove the notification observers in the right place. 并确保在正确的位置添加和删除通知观察者。

You can add observer in viewDidLoad and remove observer in viewDidUnLoad . 您可以在viewDidLoad添加观察者,并在viewDidUnLoad删除观察者。

Their might be one more issue when using this view scroll with textFields, if the orientation is supported this issue can occur. 当此视图滚动与textFields一起使用时,它们可能是另一个问题,如果支持方向,则可能会发生此问题。

Let me know if you tracked this issue, will help you. 如果您跟踪了此问题,请告诉我,将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM