Xcode 使用键盘iOS8移动UIToolbar
我的ViewController底部有一个UIToolbar,它是从Xcode 使用键盘iOS8移动UIToolbar,xcode,ios8,uitoolbar,Xcode,Ios8,Uitoolbar,我的ViewController底部有一个UIToolbar,它是从self.navigationController继承的。我将它显示在我的ViewController中的视图将出现:方法使用以下代码: [self.navigationController setToolbarHidden:NO animated:NO]; self.navigationController.toolbar.backgroundColor = [UIColor blackColor]; self.navigat
self.navigationController
继承的。我将它显示在我的ViewController中的视图将出现:
方法使用以下代码:
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.backgroundColor = [UIColor blackColor];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
之后,我的ViewController以编程方式添加了一个UITextView和一个UIButton,允许用户向ViewController(实际上是一个UITableViewController)添加注释。每当我的UITextView开始编辑时,我希望整个UIToolbar向上移动到键盘的最顶端
为了实现这一点,我有一个
-在viewDidLoad:
方法中将以下观察者添加到我的ViewController中
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
及(二)
-添加了以下方法来控制navigationController固有UIToolbar的重新定位
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is showing so resize the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame; //self.view.frame
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is hiding reset the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.origin.y += self.navigationController.toolbar.frame.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
我已经尝试了各种各样的方法来让它工作,我发现这种方法对于我的应用来说是最简单的。我得到一个错误:
分配到只读属性
在这一行:
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
在键盘中,将显示:
方法。看来我无法更改navigationController固有工具栏的位置,这是否意味着我需要以编程方式创建UIToolbar
此外,如果可能的话,我如何简单地使这段代码实现我的目标而不使事情复杂化并创建一个编程工具栏,我宁愿使用固有的工具栏。谢谢。首先:
self.view没有工具栏,您必须单独移动工具栏
第二:
self.navigationController.toolbar = CGRectMake..
这是错误的,self.navigationController.toolbar.frame=CGRectMake..
是正确的方法
第三:
使用self.navigationController.toolbar.frame.origin.y-keyboardFrame.size.height
我认为您必须根据键盘的高度正确计算self.navigationController.toolbar
的框架,并设置动画
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[self.navigationController.toolbar setFrame:CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y - YourKeyboardFrame.size.height,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height)];
[UIView commitAnimations];
另一个选项是使用UITextFields
-inputAccessoryView
(如果您使用UITextField),如果您有自己的工具栏(以编程方式制作/自定义)谢谢,这很有效,只需编辑一次。。它只需要是(-YourKeyboardFrame.size.height
)嘿,你能给我一点关于KeyboardWillHide方法中代码的细节吗?我也有同样的问题,但每次调用keyboardWillhide时,我的self.view都会向下移动。@ilia510,您只需反转keyboardWillShow
(+您的KeyboardFrame.size.height
)中的内容即可。