ios - 隐藏键盘时Blackview来了

标签 ios objective-c keyboard uitextview

这是我为在用户开始在 TextView 中输入时移动 TextView 而编写的一些代码。

这是代码,

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if(textView == _textViewMsg || textView == _subjectView ){
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
   // return YES;
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if(textView == _textViewMsg || textView == _subjectView ){
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    }
    return YES;
}


- (void)keyboardDidShow:(NSNotification *)notification
{

    [self.view setFrame:CGRectMake(0,-50,320,460)];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.view setFrame:CGRectMake(0,60,320,520)];
}

enter image description here

当我在移动中单击 TextView (在主题中) View 时,我可以在 TextView 中键入内容。它工作正常。 (图 1)

当我点击完成按钮时,同时隐藏,即在键盘隐藏时,一个黑色 View 出现(检查图像 2)几秒钟,然后再次出现正常 View 。

编辑:

解决方案:@michael 刚刚给我解决方案

UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification 它对我有用。

出现新问题: 当我开始输入前 2 个 TextView 时,即请求者、名字和姓氏......我无法输入它,因为它正在向上移动。

最佳答案

这是因为当系统动画完成时,您在键盘消失后更改了 View 的框架。快速修复:将 UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification

但如果您不介意的话,我想指出您代码中的其他几个项目。

1: 每次用户开始编辑时,您都在订阅通知。这意味着当用户第二次开始输入时,您的代码将被触发两次。在 viewDidAppear 上订阅通知和在 viewWillDisappear 上取消订阅更合适:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

2: 然后您将框架更改为恒定值。但现在苹果支持不同尺寸的不同键盘,更不用说键盘尺寸在不同设备和不同可能模式下有所不同。因此,从通知中获取键盘的大小更为明智:

- (void)keyboardDidShow:(NSNotification *)aNotification
{
    CGRect keyboardFrame = [[aNotification.userInfo valueForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect overlap =  CGRectIntersection( self.view.frame, keyboardFrame);
    // Setup new frmae according to overlap, for example reduce size by half of overlap, and move up by another half
    CGRect newFrame = self.view.frame;
    newFrame.origin.y -= overlap.size.height / 2.0
    newFrame.size.height -= overlap.size.height / 2.0
    self.view.frame = newFrame;
}

3: 动画:您也可以从通知中获取键盘动画持续时间: UIKeyboardAnimationDurationUserInfoKey:

NSTimeInterval duration = [[aNotification.userInfo  valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
    self.view.frame = newFrame;
}];

4 由于您似乎在使用 ScrollView (UITableView 和 UICollectioView 是 ScrollView ),因此您可以不更改框架,而是更改内容插入(向底部 ScrollView 添加空白区域)

[self.view setContentInset:UIEdgeInsetsMake(0, 0, overlap.size.height, 0)];

5 考虑使用自动布局。

关于ios - 隐藏键盘时Blackview来了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182614/

相关文章:

ios - JetBrains AppCode 如何启动 iOS 模拟器?

ios - 需要一种反向 CATransform3d

linux - XGrabKey 不工作

javascript - 有没有直接的方法来区分键盘按键?比如 key 的类型?

ios - MKMapView 中 showsUserLocation 的不稳定行为

ios - iOS 上的动画持续时间

iphone - 移动与触摸有关的 UIView

iphone - 在文本字段上崩溃单击 iOS 6.0 和 xcode 4.5

objective-c - 在 Swift/Objective-C 中监听窗口调整大小事件

eclipse - 无法在 Eclipse 中为 C/C++ 插入波浪号