如何让view随着键盘移动

常见的一个功能,让控件随着Keyboard上下移动而移动,实现方法很多,下面是一个比较方便的方法:

#pragma mark - 键盘改动的时候其他view随着变化
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];

}

-(void)keyboardShow:(NSNotification *)note
{
    NSLog(@"show");
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat deltaY=keyBoardRect.size.height;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  self.yourview=CGAffineTransformMakeTranslation(0, -deltaY);
    }];
}
-(void)keyboardHide:(NSNotification *)note
{
    NSLog(@"hide");
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
       self.yourview = CGAffineTransformIdentity;
    }];
}//点击返回键
上一篇:shell编程03【基本语法-运算符】


下一篇:今天没白过之《Linux的变量》