0

在 iOS 5 中,iPad 支持 3 种不同的键盘(正常、拆分、上移)。以前当键盘出现时,控制器将通过 KeyboardDidShowNotification 得到通知。在这里,如果我们有任何被键盘隐藏的 UI 元素将设置一个偏移量并向上推动元素(通过使用滚动视图)。在 iOS 5 中我们必须根据键盘的类型来处理。我们如何知道键盘类型。我们可以为新的键盘类型做什么?

谢谢,杜莱。

4

2 回答 2

1

如果您想在单击编辑文本元素时自动滚动键盘下方隐藏的 textView 或 textField 元素,以下代码将为您提供 ios5 帮助:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (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 app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:_activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _activeField = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    [self registerForKeyboardNotifications];
}
于 2013-10-23T09:00:19.433 回答
0

如果您做出反应,UIKeyboardWillShowNotification或者UIKeyboardWillHideNotification您应该没问题,因为它们仅在键盘显示为“正常模式”时发送..如果用户将其拉起或拆分,您将收到一个UIKeyboardWillHideNotification(奇怪的行为,但苹果唯一的选择)向后兼容 iOS 4 应用程序)

于 2011-10-21T07:34:33.193 回答