ios 当键盘就位时移动 UIScrollView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8590636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:58:41  来源:igfitidea点击:

Move UIScrollView when keyboard comes into place

iphoneobjective-ciosipadkeyboard

提问by thenetimp

[Edit:] The problem has been solved. I did not have my delegates linked properly in UIBuilder. Code is good!

[编辑:] 问题已解决。我没有在UIBuilder. 代码很好!

I am trying to resize a scrollview when the keyboard appears. I went to the developer docs and found this information.

当键盘出现时,我试图调整滚动视图的大小。我去了开发人员文档并找到了这些信息。

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1

On the left "Managing the keyboard".

在左侧“管理键盘”。

In the documentation it shows a bit of code to detect the size of the keyboard and then to resize a UIScrollView. I have placed an NSLogmessage in the code for function - (void)keyboardWasShown:(NSNotification*)aNotificationso I see that the function is actually being called, but when I try to to NSLogthe kbSize.height it is always valued at 0.

在文档中,它显示了一些代码来检测键盘的大小,然后调整UIScrollView. 我NSLog在函数的代码中放置了一条消息,- (void)keyboardWasShown:(NSNotification*)aNotification所以我看到该函数实际上正在被调用,但是当我尝试NSLog使用kbSize.height 时,它的值始终为 0。

Why does the code that apple provide for this purpose not work?

为什么苹果为此目的提供的代码不起作用?

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

回答by Reuven

You may want to try the highly recommended "TPKeyboardAvoidingScrollView", available from: https://github.com/michaeltyson/TPKeyboardAvoiding

您可能想尝试强烈推荐的“TPKeyboardAvoidingScrollView”,可从以下网址获得:https: //github.com/michaeltyson/TPKeyboardAvoiding

Works like a charm...

奇迹般有效...

回答by Mihai Fratu

Have you ever added an observer for that specific notification? Make sure that in your loadViewmethod you do this:

您是否为该特定通知添加了观察者?确保在你的loadView方法中你这样做:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

Don't forget to unregister the observer on viewDidUnloadmethod like this:

不要忘记在viewDidUnload方法上取消注册观察者,如下所示:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Let me know if that works out!

让我知道这是否有效!

回答by DBD

I've done this several times with a UITableView(which is just an extended UIScrollView). You can find the code in this answer.

我已经用 a UITableView(这只是一个扩展UIScrollView)做了几次。您可以在此答案中找到代码

回答by bogen

One simple solution is to add the extension UIViewController+Keyboard.swift to your project, with a single line

一个简单的解决方案是将扩展 UIViewController+Keyboard.swift 添加到您的项目中,只需一行

setupKeyboardNotifcationListenerForScrollView(scrollView)

it will auto resize automatically when keyboard appears. No need to subclass anything, just an extension! Its availble at GitHub SingleLineKeyboardResize

当键盘出现时它会自动调整大小。不需要继承任何东西,只是一个扩展!它在 GitHub SingleLineKeyboardResize上可用