iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS虚拟键盘大小没有通知中心大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当需要点击虚拟键盘的textFiewl时,我需要向上滚动我的scrollView.我调用[self.scrollView setContentOffset:scrollPoint animated:YES] ;.要获得屏幕的可见区域,我显然需要KB大小.

我很熟悉

NSDictionary *info = [notification userInfo];

CGSize kbSize = [self.view convertRect:
                 [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue]
                              fromView:nil].size;

但是,它对我不起作用,因为当用户点击可能半隐藏的文本字段时,我没有收到键盘通知.

所以我调用textFieldDidBeginEditing:中的方法,在键盘发送消息之前调用它,所以我不知道第一次点击时的KB大小.

所以问题是:是否可以获得KB大小,而无需调用相应的通知
Programmaticaly,而不是硬编码.

解决方法

你做错了.

您还需要听取键盘显示/隐藏通知,然后调整屏幕.

这是一个示例框架代码

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardChangedStatus:) name:UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardChangedStatus:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - Get Keyboard size

- (void)keyboardChangedStatus:(NSNotification*)notification {
    //get the size!
    CGRect keyboardRect;
    [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardRect];
    keyboardHeight = keyboardRect.size.height;
    //move your view to the top,to display the textfield..
    [self moveView:notification keyboardHeight:keyboardHeight];
}

#pragma mark View Moving

- (void)moveView:(NSNotification *) notification keyboardHeight:(int)height{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = self.view.frame;

    if ([[notification name] isEqual:UIKeyboardWillHideNotification]) {
        // revert back to the normal state.
        rect.origin.y = 0;
        hasScrolledToTop = YES;
    } 
    else {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard (you need to adjust the value here)
        rect.origin.y = -height;
    }

    self.view.frame = rect;

    [UIView commitAnimations];
}

大佬总结

以上是大佬教程为你收集整理的iOS虚拟键盘大小没有通知中心全部内容,希望文章能够帮你解决iOS虚拟键盘大小没有通知中心所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: