如何让view随着键盘移动

简介: 常见的一个功能,让控件随着Keyboard上下移动而移动,实现方法很多,下面是一个比较方便的方法:#pragma mark - 键盘改动的时候其他view随着变化-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter de

常见的一个功能,让控件随着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;
    }];
}//点击返回键
相关文章
|
24天前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
XML 数据安全/隐私保护 Android开发
android如何让布局一直在键盘上方显示
在实际项目中,肯定会有输入数据的情况,这样就会用到键盘。
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
359 0
HMI-5-[QtKeyEvent]:解决Qt键盘事件无法获取到上下左右等按键并实现键盘解析
|
Android开发
EditText(防止进入页面就获取焦点弹出键盘)
(创建于2016/11/7) 只需要在该页面所在的activity的manifest中添加 android:windowSoftInputMode="adjustPan|stateHidden" stateHidden是为了隐藏键盘,adjustPa...
1035 0
UWP的TextBox和PasswordBox使用输入范围更改触摸键盘InputScope
原文:UWP的TextBox和PasswordBox使用输入范围更改触摸键盘InputScope 当你的应用运行在具有触摸屏的设备上时,触摸键盘可用于文本输入。当用户点击可编辑的输入字段(如 TextBox 或 PasswordBox)时,系统会调用触摸键盘。
1167 0