我遇到了一个问题:
我有一个像这样的视图控制器。
要在键盘出现时使工具栏向上移动,我将self.view向上移动。
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height * (up ? -1 : 1), self.view.frame.size.width, self.view.frame.size.height)];
现在我想点击工具栏中的左键,然后显示该框架与此键盘相同的视图。
但是我可以在哪里添加此视图? 如果将子视图添加到self.view,它将向上移动键盘顶部的self.view,而不是封面。 我是IOS的初学者,我不知道它,并且已经搜索过,但对此一无所知。
另一个问题,如果工具栏在底部,我也想点击它上面的左键来显示视图(动画和帧都和键盘一样),我该怎么办?
你能帮帮我吗? 感谢答案 0 :(得分:1)
我是对的,您想要使用键盘上下键盘上方的键盘?
然后你必须注册键盘显示和隐藏通知,并根据通知和键盘显示和隐藏速度来上下调整视图。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
现在你必须实现所有选择器方法:
//Code from Brett Schumann
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
// get a rect for the textView frame
CGRect containerFrame = containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
containerView.frame = containerFrame;
// commit animations
[UIView commitAnimations];
[self showSendButton];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// get a rect for the textView frame
CGRect containerFrame = containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
containerView.frame = containerFrame;
// commit animations
[UIView commitAnimations];
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, containerView.frame.size.height-46.0f+20.0f, 0);
_table.contentInset = contentInsets;
_table.scrollIndicatorInsets = contentInsets;
//display button based on facebook login status
if ([facebookService.facebook isSessionValid]) {
[commentButton makeButtonLogoutButton];
textView.editable = YES;
} else {
[commentButton makeButtonLoginButton];
textView.editable = NO;
}
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+20.0f, 0.0);
_table.contentInset = contentInsets;
_table.scrollIndicatorInsets = contentInsets;
}
使用您自己的视图轻轻替换键盘顶部的给定视图。该视图应该是包含所有按钮和内容的单个视图。
问候
答案 1 :(得分:0)
首先,为什么要覆盖键盘?看起来这不是一个好主意。而且我认为它根本不可能。
对于第二个问题的答案,您应该查看UIView
文档:
[UIView animateWithDuration:0.3 animations:^{
// place your view wherever you want
CGRect frame = myView.frame;
frame.y = self.view.frame.size.height - frame.size.height;
myView.frame = frame;
}];