上移 UIToolbar

move up UIToolbar(上移 UIToolbar)
本文介绍了上移 UIToolbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的视图底部有一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Numeric Soft Keyboard on Android(Android 上的数字软键盘)