2017-02-20 263 views
1

我这样的代码:键盘不显示(IOS)

- (void)setupSubViews { 
    [self addSubview:self.textView]; 
    [self addSubview:self.sendButton]; 

    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(5); 
     make.bottom.equalTo(-5); 
     make.leading.equalTo(9); 
    }]; 
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.centerY.equalTo(self); 
     make.trailing.equalTo(-9); 
     make.leading.equalTo(self.textView.mas_trailing).offset(7); 
     make.width.equalTo(60); 
     make.height.equalTo(40); 
    }]; 
} 

展示功能,使TextView的becomeFirstResponder

- (void)show { 
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 
    [keyWindow addSubview:self]; 
    [self mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.leading.trailing.equalTo(0); 
     make.bottom.equalTo(InputHeight); 
    }]; 
    [self.textView becomeFirstResponder]; 
} 

此视图添加到keyWindow在mainScreen底部,当键盘的框架改变时,翻译会随着键盘改变。

- (void)keyboardWillChangeFrame:(NSNotification *)noti { 
    if (!self.textView.isFirstResponder) { 
     return; 
    } 
    NSValue *endFrame = noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"]; 
    CGFloat endY = [endFrame CGRectValue].origin.y; 
    BOOL isBackToBottom = endY == UI_SCREEN_HEIGHT; 
    CGFloat needOffset = endY - (UI_SCREEN_HEIGHT + InputHeight); 

    if (isBackToBottom) { 
     [UIView animateWithDuration:0.25 animations:^{ 
      self.transform = CGAffineTransformIdentity; 
     } completion:^(BOOL finished) { 
      [self removeFromSuperview]; 
     }]; 
    } else { 
     [UIView animateWithDuration:0.25 animations:^{ 
      self.transform = CGAffineTransformMakeTranslation(0, needOffset); 
     }]; 
    } 
} 

使用案例: 的viewController有一个按钮,点击按钮的动作是这样的:

- (void)beginChat { 
    [self.inputView show]; 
} 

self.inputView是上述customView,

- (LiveChatInputView *)inputView { 
    if (!_inputView) { 
     _inputView = [LiveChatInputView new]; 
     _inputView.sendBlock = ^(NSString *string) { 
      .... 
     }; 
    } 
    return _inputView; 
} 

现在我的问题是,当我们在应用程序启动后第一次调用show函数,键盘将不会显示,但在第二次调用时调用show函数我,每件事情都很好。

如何处理这个问题,THX。

+0

第一次调用show函数时检查self.textView是否为零。 –

+0

您正在从设备或模拟器进行测试吗?在SIM卡上它有一个常见的问题,即键盘有时无法正常工作。 – zero3nna

+0

@Fahad Jamal我确定self.textView不是零 – Archerlly

回答

0

我犯了一个错误,在keyboardWillChangeFrame:函数调用[self removeFromSuperview]

我以为UIKeyboardWillChangeFrameNotification只能在键盘上表演一次张贴或隐藏,所以我放回行动的实施keyboardWillChangeFrame:,并通过endFrame.origin.y == UI_SCREEN_HEIGHT

在实际控制,但事实是: 当键盘显示,NSNotificationCenter将发布UIKeyboardWillChangeFrameNotification三次:

第一 enter image description here 第二 enter image description here 第三 enter image description here

提示: 仅第一次显示应用推出后的键盘,三个以上的第一UIKeyboardWillChangeFrameNotification,该endFrame.origin.y等于UI_SCREEN_HEIGHT

我的解决办法: 观察UIKeyboardWillHideNotification,并在此回调中执行回退操作。

感谢这个问题的所有答案。

0

调用此方法并不能保证该对象将成为第一响应者。

苹果

Responder对象只能成为在当前 响应者辞职第一响应状态(canResignFirstResponder) 和新的响应可以成为第一个响应的第一个响应者。

尝试调用becomeFirstResponder像下面,

[self.textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0]; 
+0

THX,但它不适合我。在'[self.textView becomeFirstrespond]'之前',self.textView.canBecomeFirstResponder返回yes – Archerlly