如何在键盘出现时在iOS上调整UITextView的大小? [英] How to resize UITextView on iOS when a keyboard appears?

查看:131
本文介绍了如何在键盘出现时在iOS上调整UITextView的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITextView 已插入 UITabBarController 的标签中(在iPhone上)。

There's UITextView inserted into tab in UITabBarController (on the iPhone).


  1. 用很多行填写 UITextView

  2. 显示一个键盘编辑文字。

发生什么事了?键盘用光标隐藏了一半 UITextView 。无法编辑文本作为结果。

What's happen? The keyboard hide a half of UITextView with cursor. Can't edit the text as the result.

如何解决所有Apple移动设备的问题(屏幕分辨率不同)?非常感谢你的帮助!

How to resolve the issue for all Apple mobile devices (with different screen resolution)? Thanks a lot for help!

推荐答案

多年过去了,问题仍然存在。 Apple绝对应该自己处理所有这些事情。但事实并非如此。以下是基于官方Apple的新解决方案文档以及错误修复。它支持iOS 8,iOS 9,inputAccessoryView,适用于新版本的iOS和新设备。

Years gone, the question is still actual. Apple definitely should handle all these things itself. But it doesn't. Here's the new solution based on the official Apple's documentation plus bug fixes. It supports iOS 8, iOS 9, inputAccessoryView and is ready for new versions of iOS and new devices.

/* Apple's solution to resize keyboard but with accessory view support */

- (void)keyboardDidShow:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double keyboardHeight = [[UIScreen mainScreen] bounds].size.height - keyboardFrame.origin.y;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    editor.contentInset = contentInsets;
    editor.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification*)aNotification {
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    editor.contentInset = contentInsets;
    editor.scrollIndicatorInsets = contentInsets;

    // button to hide the keyboard
    buttonDone.enabled = false;
}

/* Fix issues with size classes and accessory view */

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    // fix incorrect size of the inputAccessoryView when size class changed
    // willTransitionToTraitCollection and traitCollectionDidChange can't help us
    if (editor && editor.inputAccessoryView && !editor.inputAccessoryView.hidden) {
        [editor resignFirstResponder];
    }
}

/* Hide accessory view if a hardware keyboard is present */

#define gThresholdForHardwareKeyboardToolbar 160.f // it's minimum height of the software keyboard on iPhone 4 in landscape mode

- (bool)isExternalKeyboard:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double keyboardHeight = [[UIScreen mainScreen] bounds].size.height - keyboardFrame.origin.y;

    return keyboardHeight < gThresholdForHardwareKeyboardToolbar;
}

- (void)keyboardWillShow:(NSNotification*)aNotification {
    if ([self isExternalKeyboard:aNotification]) {
        // hardware keyboard is present
        if (editor && editor.inputAccessoryView) {
            editor.inputAccessoryView.hidden = true;
        }
    } else {
        // only on-screen keyboard
        if (editor && editor.inputAccessoryView) {
            editor.inputAccessoryView.hidden = false;
        }
    }

    // button to hide the keyboard
    buttonDone.enabled = true;
}

这篇关于如何在键盘出现时在iOS上调整UITextView的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆