Ios 当键盘出现时,如何向上移动两个UIView?
我有两个UIView,UIView2得到了UIExtView,它可以放在用户喜欢添加文本标签的任何地方。当用户将UiTextView放在底部时,键盘显示为键入文本,UiTextView向上移动。而且效果很好!我还需要移动UIView1,它位于UIView1下 UIView2是UIView1的属性,当UIView2的UIExtView成为第一响应者时,我需要通知UIView1为其执行上移方法Ios 当键盘出现时,如何向上移动两个UIView?,ios,uiview,nsnotificationcenter,Ios,Uiview,Nsnotificationcenter,我有两个UIView,UIView2得到了UIExtView,它可以放在用户喜欢添加文本标签的任何地方。当用户将UiTextView放在底部时,键盘显示为键入文本,UiTextView向上移动。而且效果很好!我还需要移动UIView1,它位于UIView1下 UIView2是UIView1的属性,当UIView2的UIExtView成为第一响应者时,我需要通知UIView1为其执行上移方法 [[NSNotificationCenter defaultCenter] addObserver:se
[[NSNotificationCenter defaultCenter] addObserver:self.drawingView
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self.drawingView
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
您已经为键盘事件添加了通知,然后您要做的是实现keyboardWillShow和keyboardWillHide方法。见下文
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[self moveCommentBarViewWithKeyBoardHeight:keyboardRect.size.height withDuration:animationDuration];
}
希望它能帮上忙:)
PS:添加此通知时,最好添加到方法-(void)viewwillbeen:(BOOL)animite。和使用
[[NSNotificationCenter defaultCenter] removeObserver:self];
要移除视图,请观察视图何时消失
-(void)moveCommentBarViewWithKeyBoardHeight:(CGFloat)kHeighy withDuration:(NSTimeInterval)animationD
{
CGRect tempRect = commentEditedBarView.frame;
[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:animationD];
[commentEditedBarView setFrame:CGRectMake(tempRect.origin.x, self.view.frame.size.height-kHeighy-tempRect.size.height, tempRect.size.width, tempRect.size.height)];
[UIView commitAnimations];
}
控制器需要处理通知,而不是视图。然后控制器移动视图。你看过这个问题的无数其他答案了吗?查看相关问题列表。谢谢,但我有keyboardWillShow和keyboardWillHide的方法。我通过NSNotification从UIView1调用UIView2的这些方法。但是我尝试在UIView1中执行相同的操作,但是UITextView属于UIView2。不,这不是您所拥有的,而是您必须实现您的方法。看看我给你的那些方法。好的,我忘记给你移动视图的方法了。我现在添加了我的答案。正如我所说,它对UIView2(self.drawingView)非常有效,但我无法移动UIView2查看我的方法。你可以像这样重写我的方法-(void)moveCommentBarViewWithKeyBoardHeight:(UIView*)查看你要移动的textField的superview,这样无论你把textView放在哪里,你都可以让superview移动。非常感谢!)每个人
-(void)moveCommentBarViewWithKeyBoardHeight:(CGFloat)kHeighy withDuration:(NSTimeInterval)animationD
{
CGRect tempRect = commentEditedBarView.frame;
[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:animationD];
[commentEditedBarView setFrame:CGRectMake(tempRect.origin.x, self.view.frame.size.height-kHeighy-tempRect.size.height, tempRect.size.width, tempRect.size.height)];
[UIView commitAnimations];
}