出现键盘时如何在iOS上调整UITextView的大小?

时间:2023-02-01
本文介绍了出现键盘时如何在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 官方 文档加上错误修复.它支持 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的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何可靠地检测 iOS 9 上是否连接了外部键盘? 下一篇:EmptyThrowable:磁盘上不存在 APK 文件 *.apk

相关文章

最新文章