键盘到位时移动 UIScrollView

Move UIScrollView when keyboard comes into place(键盘到位时移动 UIScrollView)

本文介绍了键盘到位时移动 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已解决.我没有在 UIBuilder 中正确链接我的代表.代码很好!

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

我正在尝试在键盘出现时调整滚动视图的大小.我去了开发者文档并找到了这些信息.

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

左侧的管理键盘".

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

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 NSLog message in the code for function - (void)keyboardWasShown:(NSNotification*)aNotification so I see that the function is actually being called, but when I try to to NSLog the kbSize.height it is always valued at 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];
    }
}

推荐答案

您可能想尝试强烈推荐的TPKeyboardAvoidingScrollView",可从:https://github.com/michaeltyson/TPKeyboardAvoiding

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

像魅力一样工作......

Works like a charm...

这篇关于键盘到位时移动 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:键盘到位时移动 UIScrollView

基础教程推荐