通过通知监听键盘的状态来改变View的位置

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>{
    UIView * _mainView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
    [self.view addSubview:_mainView];
    _mainView.backgroundColor = [UIColor orangeColor];
    
    // 新建一个UITextField,位置及背景颜色随意写的。
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
    textField.delegate = self;
    textField.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:textField]; // 自定义的view
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPosition:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return  YES;
}

// 根据键盘状态,调整_mainView的位置

- (void)changeContentViewPosition:(NSNotification *)notification{
    
    NSDictionary *userInfo = [notification userInfo];
    
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // 得到键盘弹出后的键盘视图所在y坐标
    CGFloat keyBoardEndY = value.CGRectValue.origin.y;
    // 键盘弹出所用时间
    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    // 添加移动动画,使视图跟随键盘移动
    [UIView animateWithDuration:duration.doubleValue animations:^{ [UIView setAnimationBeginsFromCurrentState:YES];
        // 对View设置转场动画方向 枚举类型
        [UIView setAnimationCurve:[curve intValue]];
        _mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - 20 - _mainView.bounds.size.height/2.0);  // keyBoardEndY的坐标包括了状态栏的高度,要减去

}];
}

-(void)dealloc{
    //移除监听
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end

时间: 2024-08-06 11:00:00

通过通知监听键盘的状态来改变View的位置的相关文章

Android通过监听最外层布局的改变监听键盘的状态,软键盘的弹出和收起都会改变外层布局(前提是把Activity的mode设置成压缩);

最外层布局.addOnLayoutChangeListener(new OnLayoutChangeListener() { @Override public void onLayoutChange(View arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8) { // TODO Auto-generated method stub if(EditText.hasFocus()

iOS开发中如何在键盘弹出时改变View的高度

在iOS开发的时候有两个经常要用到的控件UITextfield跟UITextView,我们输入内容基本是通过这两个控件进行的,但是有时候会遇到这样的问题:在点击输入之后弹出键盘遮盖住了输入框,可以通过以下办法解决: 添加通知监听键盘的弹出跟隐藏 //监听键盘弹出和隐藏 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillSho

21-30(NSTimer定时器 Cell的重用原理 代理的使用场合 UITableViewCell结构 监听键盘的通知)

21.NSTimer定时器 22.tableView的基本用法 23.tableView的常用属性 24.Cell的重用原理: 25.UITableViewCell结构 26.使用xib封装一个view的步骤 27.代理的使用场合 28.使用delegate的步骤 29.通过代码自定义cell步骤 30.监听键盘的通知 { 细节决定成败, 这句话讲的太对了, 所以我们要注意每一个细节!今天还好注意了, 没犯错!嘿嘿! 今天心情特别好, 心情好! 啥都好! 给大家来个笑, 工作的同时,不要忘记笑容

IOS 监听键盘的通知

通知方法: /** * 当键盘改变了frame(位置和尺寸)的时候调用 */ - (void)keyboardWillChangeFrame:(NSNotification *)note { // 设置窗口的颜色 self.view.window.backgroundColor = self.tableView.backgroundColor; // 0.取出键盘动画的时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDuration

监听键盘通知

//监听键盘状态     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];     //监听输入法状态     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chang

注册监听键盘事件的通知

注册监听键盘事件的通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIK

为什么不要在viewDidLoad方法中设置开始监听键盘通知

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个普遍的错误是,程序猿(媛)试图在view controller的view不在屏幕上时监听键盘通知. 他们开始在viewDidLoad方法中开始监听通知,并且在dealloc方法中移除监听通知. 这是一种大有问题的行为,因为当你的视图不在屏幕上显示,而键盘开始显示在其他视图上的时候,你绝不应该在你的视图控制器中调整任何键盘组件的显示! 牢记这一点:键盘通知就

通过KVO来监听键盘弹出和弹回

在通知中心建立一个广播来监听键盘的弹出和弹回,在监听事件中加入触发事件的一些操作. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self sele

监听键盘事件

注册监听键盘事件: 1 // 键盘即将隐藏 2 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 3 4 // 键盘已经隐藏 5 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sel