Objective C 视图不随键盘移动
Objective C View Not Moving With Keyboard
我在我的应用程序的文本视图上使用以下代码。我已经做到了,当键盘触发时,视图将出现在键盘顶部,当我在键盘和文本视图外部单击时,它将关闭键盘。我遇到的问题是,当我单击 textview 时,textview 在键盘顶部移动有大约 5 秒的延迟,然后弹出,当我单击以关闭键盘时,它会立即消失并且不会像动画一样键盘打开。有什么方法可以解决此问题并使视图在键盘打开和关闭时在键盘顶部移动而不是跳跃?
谢谢
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(didShow:) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
_theTextView.delegate = self;
_theTextView.text = @"Type your message..";
_theTextView.textColor = [UIColor lightGrayColor]; //optional
_theTextView.layer.cornerRadius = 5;
_theTextView.layer.masksToBounds = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[_theTextView resignFirstResponder];
}
- (void)didShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.textViewPOS.constant = keyboardSize.height;
NSLog(@"Opened");
}
- (void)didHide
{
self.textViewPOS.constant = 0;
NSLog(@"Closed");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"typing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"Type your message.."]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"Type your message..";
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
@end
对于延迟问题,您可以按照 Eugene Zaychenko 的建议使用 UIKeyboardWillShowNotification
。它会解决你的问题。
[center addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
对于动画使用下面的代码
- (void)didHide
{
self.textViewPOS.constant = 0;
NSLog(@"Closed");
[UIView animateWithDuration:1 delay:0 options:0 animations:^{
self.textViewPOS.constant = 0;
[self.view layoutIfNeeded];
} completion:nil];
}
- (void)willShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:1 delay:0 options:0 animations:^{
self.textViewPOS.constant = keyboardSize.height;
[self.view layoutIfNeeded];
} completion:nil];
NSLog(@"Opened");
}
如有任何疑问,请告诉我。
这种延迟是否仅在您首次执行项目时发生?
- 然后在应用程序中使用选项完成启动(在 appdeligate 中)只需使用代码制作一个文本视图。
- 然后添加我们的 view.Then 让 textview 作为第一响应者。
- 然后辞去它作为第一响应者..
- 然后最终从我们的父视图中删除 textview。
您的延迟问题将由此解决。
我在我的应用程序的文本视图上使用以下代码。我已经做到了,当键盘触发时,视图将出现在键盘顶部,当我在键盘和文本视图外部单击时,它将关闭键盘。我遇到的问题是,当我单击 textview 时,textview 在键盘顶部移动有大约 5 秒的延迟,然后弹出,当我单击以关闭键盘时,它会立即消失并且不会像动画一样键盘打开。有什么方法可以解决此问题并使视图在键盘打开和关闭时在键盘顶部移动而不是跳跃?
谢谢
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(didShow:) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
_theTextView.delegate = self;
_theTextView.text = @"Type your message..";
_theTextView.textColor = [UIColor lightGrayColor]; //optional
_theTextView.layer.cornerRadius = 5;
_theTextView.layer.masksToBounds = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[_theTextView resignFirstResponder];
}
- (void)didShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.textViewPOS.constant = keyboardSize.height;
NSLog(@"Opened");
}
- (void)didHide
{
self.textViewPOS.constant = 0;
NSLog(@"Closed");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"typing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@"Type your message.."]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"Type your message..";
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
@end
对于延迟问题,您可以按照 Eugene Zaychenko 的建议使用 UIKeyboardWillShowNotification
。它会解决你的问题。
[center addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
对于动画使用下面的代码
- (void)didHide
{
self.textViewPOS.constant = 0;
NSLog(@"Closed");
[UIView animateWithDuration:1 delay:0 options:0 animations:^{
self.textViewPOS.constant = 0;
[self.view layoutIfNeeded];
} completion:nil];
}
- (void)willShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:1 delay:0 options:0 animations:^{
self.textViewPOS.constant = keyboardSize.height;
[self.view layoutIfNeeded];
} completion:nil];
NSLog(@"Opened");
}
如有任何疑问,请告诉我。
这种延迟是否仅在您首次执行项目时发生?
- 然后在应用程序中使用选项完成启动(在 appdeligate 中)只需使用代码制作一个文本视图。
- 然后添加我们的 view.Then 让 textview 作为第一响应者。
- 然后辞去它作为第一响应者..
- 然后最终从我们的父视图中删除 textview。
您的延迟问题将由此解决。