用户登录
用户注册

分享至

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

  • 作者: 迷离的夜色53222243
  • 来源: 51数据库
  • 2023-02-09

问题描述

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;
}
软件
前端设计
程序设计
Java相关