iphone - 键盘到位时移动 UIScrollView

标签 iphone objective-c ios ipad keyboard

[编辑:]问题已经解决。我没有在 UIBuilder 中正确链接我的委托(delegate)。代码很好!

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

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。

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

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

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

关于iphone - 键盘到位时移动 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590636/

相关文章:

iphone - iOS UIScrollView 延迟加载

ios - UITextField 只允许字母和数字

ios - UISplitViewController - 主视图在纵向模式下隐藏时的事件?

ios - 使用 Wowza 媒体服务器在 iOS 中直播

iphone - 我的应用程序无法在我的设备中启动?

ios - iOS 7.0.3 中的条形半透明消失了

ios - 需要 xcdatamodel 吗?

iphone - 根据纬度和经度得到错误的排序距离

ios - 点击或长按单元格下方

ios - 从 iOS 设备上的 Raspberry Pi 流式传输 H.264 视频