我的代码是这样的:
final int MY_COLOR = R.color.childArticle;
View v;
v.setBackgroundColor(context.getResources()
.getColor(R.color.childArticle));
view.setTag(R.color.childArticle);
Integer colorOfSelectedRow = (Integer) v.getTag();
if(colorOfSelectedRow == MY_COLOR) {
// Do something
}
show函数使textView成为FirstRirstResponder
- (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);
}];
}
此视图添加到mainScreen底部的keyWindow,当键盘的框架发生变化时,它的翻译将随着键盘的变化而变化。
- (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];
}
用例: viewController有一个按钮,按钮点击动作如下:
- (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);
}];
}
}
self.inputView是上面的customView,
- (void)beginChat {
[self.inputView show];
}
现在我的问题是,当我们在应用程序启动后第一次调用- (LiveChatInputView *)inputView {
if (!_inputView) {
_inputView = [LiveChatInputView new];
_inputView.sendBlock = ^(NSString *string) {
....
};
}
return _inputView;
}
函数时,键盘将不显示,但是第二次调用show
函数,每件事都没问题。
如何处理这个问题,THX。
答案 0 :(得分:0)
调用此方法并不能保证对象将成为第一个响应者。
根据 Apple ,
如果当前,响应者对象只成为第一个响应者 响应者可以辞退第一响应者状态(canResignFirstResponder) 并且新的响应者可以成为第一响应者。
尝试像下面一样调用 becomeFirstResponder ,
[self.textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];
答案 1 :(得分:0)
我犯了一个错误,在[self removeFromSuperview]
的函数中调用keyboardWillChangeFrame:
。
我认为UIKeyboardWillChangeFrameNotification
只能在键盘显示或隐藏时发布一次,所以我将后退操作的执行放在keyboardWillChangeFrame:
中,并由endFrame.origin.y == UI_SCREEN_HEIGHT
控制
在实践中,事实是:
当键盘显示时,NSNotificationCenter
将发布UIKeyboardWillChangeFrameNotification
三次:
<强>提示:强>
只有在应用程序启动后第一次显示键盘时,{3}中的第一个UIKeyboardWillChangeFrameNotification
等于endFrame.origin.y
我的解决方案:
观察UI_SCREEN_HEIGHT
,并在此回调中执行后退操作。
感谢这个问题的所有答案。