#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

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

  1. (二十四)监听键盘的通知和键盘弹出隐藏的View移动

    让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法. //监听键盘的操作: [[NSNotificationCenter def ...

  2. IOS 监听键盘的通知(NSNotificationCenter)

    通知方法: /** * 当键盘改变了frame(位置和尺寸)的时候调用 */ - (void)keyboardWillChangeFrame:(NSNotification *)note { // 设 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个普遍的错误是,程序猿(媛)试图在view controll ...

  4. Android 监听键盘的弹起与收缩

    Android 监听键盘的弹起与收缩 由于android不存在该监听的API 所以需要自己去处理 先上代码 /* android:windowSoftInputMode="stateAlwa ...

  5. iOS实时监控网络状态的改变

    在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...

  6. swift 监听键盘弹出的高度

    // 监听键盘通知 NotificationCenter.default.addObserver(self, selector: #selector(ComposeViewController.key ...

  7. Android监听键盘右下角确定键

    代码改变世界 Android监听键盘右下角确定键 kotlin写法 <EditText android:id="@+id/seachFriend" android:layou ...

  8. iOS--实时监控网络状态的改变

    在网络应用中,有的时候需要对用户设备的网络状态进行实时监控,有两个目的: (1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能) (2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体 ...

  9. 避免scrollview内部控件输入时被键盘遮挡,监听键盘弹起,配合做滚动

    1,监听键盘 2,根据当前键盘弹起高度与控件的底部位置计算滑动距离 3,根据滑动距离在键盘弹起和隐藏是分别设置动画完成滑动     实现: 1,监听键盘使用   #pragma mark - 键盘监听 ...

随机推荐

  1. XidianOJ 1041: Franky的游戏O

    题目描述 Franky是super的人造人,来到了n*m的棋盘世界玩冒险游戏. n×m的棋盘由n行每行m个方格组成,左上角的方格坐标是(0,0),右下角的方格坐标是(n-1,m-1). 每次游戏时,他 ...

  2. 多层iframe的页面取子标签

    <iframe src=''> <iframe src=''> <iframe src=''> <div></div></iframe ...

  3. 并发编程中.net与java的一些对比

    Java在并发编程中进行使用java.util.concurrent.atomic来处理一些轻量级变量 如AtomicInteger AtomicBoolean等 .Net中则使用Interlocke ...

  4. Math类中的BigDecimal

    如果我们编译运行下面这个程序会看到什么? public class Test {    public static void main(String args[]) {                 ...

  5. HTML5标签的3大类型

    1>块级标签: 独占一行的标签,能随时设置宽度和高度 比如:div.p.h1.h2.u1.li 2>行内标签(内联标签): 多个行内标签能同时显示在一行,宽度和高度取决于内容的尺寸 比如: ...

  6. 四元数quaternion

    四元数的简单方法运用四元数在Unity3D中的作用就是拿来表示旋转. AngleAxis 创建一个旋转,绕着某个轴旋转,返回结果是一个四元数. 跟ToAngleAxis实现的是相反的功能. Angle ...

  7. Texture Filter中的Bilinear、Trilinear以及Anistropic Filtering

    1. 为什么在纹理采样时需要texture filter(纹理过滤)?我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture ...

  8. ICMP Protocol

    [ICMP Protocol] 参考: 1.ICMP Types and Codes:http://www.nthelp.com/icmp.html 2.RFC 792 - Internet Cont ...

  9. 图情期刊要求2015(A,B,C类)

    中国图书馆学报+情报学报+大学图书馆学报+图书情报工作+图书情报知识+情报理论与实践+国家图书馆学刊+情报杂志+图书与情报+情报科学+图书馆杂志+图书馆建设+情报资料工作+图书馆论坛+现代图书情报技术 ...

  10. 第二章 centos安装maven

    一.官网下载 apache-maven-3.3.9-bin.tar.gz 注意:需要jdk1.7及以上 二.上传 scp apache-maven-3.3.9-bin.tar.gz root@10.2 ...