我的视图底部有一个 UIToolbar,这个工具栏中有一个 UITextField.当我开始编辑此字段时,它隐藏在键盘后面.要查看我输入的内容,我想在出现键盘时将工具栏向上移动(然后在完成编辑后将其向下移动).
如何向上/向下移动此 UIToolbar?
将你的 viewController 类添加到 UIKeyboardWillShowNotification
/UIKeyboardWillHideNotification
的观察者列表中.然后您可以移动您的视图以使您的 textView 可见.您还可以从此通知中获取动画参数,以将您的动画与当前操作系统版本的键盘动画参数同步.这段代码我用于分页
- (void) viewWillAppear:(BOOL)animated{[超级viewWillAppear:动画];[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(liftMainViewWhenKeybordAppears:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnMainViewToInitialposition:) name:UIKeyboardWillHideNotification object:nil];}- (void) viewWillDisappear:(BOOL)animated{[超级viewWillDisappear:动画];[[NSNotificationCenter defaultCenter] removeObserver:self];}
在下面的方法中,我设置了两种方法来处理键盘通知.以下是这些方法:
<块引用>- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{NSDictionary* userInfo = [aNotification userInfo];NSTimeInterval 动画持续时间;UIViewAnimationCurve 动画曲线;CGRect 键盘框架;[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];[[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:animationDuration];[UIView setAnimationCurve:animationCurve];[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.大小.高度)];[UIView 提交动画];}
- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{NSDictionary* userInfo = [aNotification userInfo];NSTimeInterval 动画持续时间;UIViewAnimationCurve 动画曲线;CGRect 键盘框架;[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];[[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:animationDuration];[UIView setAnimationCurve:animationCurve];[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.大小.高度)];[UIView 提交动画];}
I have a UIToolbar at bottom of my view, and I have a UITextField in this toolbar. When I begin editing this field, it is hidden behind the keyboard. To see what I've typed, I want to move the toolbar up at the moment the keyboard is presented (and then move it back down when I've finished editing).
How do I move this UIToolbar up/down?
add your viewController class to the list of observers of UIKeyboardWillShowNotification
/UIKeyboardWillHideNotification
. then you can move your view to make your textView visible. You can also get animation parameters from this notifications to synchronize your animation with keyboard animation parameters of the current OS version. this code I've used for paging
- (void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(liftMainViewWhenKeybordAppears:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnMainViewToInitialposition:) name:UIKeyboardWillHideNotification object:nil]; } - (void) viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; }
in the methods below I set two methods to handle keyboard notifications. and here are this methods:
- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{ NSDictionary* userInfo = [aNotification userInfo]; NSTimeInterval animationDuration; UIViewAnimationCurve animationCurve; CGRect keyboardFrame; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:animationDuration]; [UIView setAnimationCurve:animationCurve]; [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; [UIView commitAnimations]; }
- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
NSDictionary* userInfo = [aNotification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
[UIView commitAnimations];
}
这篇关于上移 UIToolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!