iOS 键盘遮挡 页面上移

适配不同型号的键盘大小

@property (nonatomic, assign)CGSize kbSize;

viewDidLoad

[self registerForKeyboardNotifications];

各种方法


- (void)registerForKeyboardNotifications{
    
    //使用NSNotificationCenter 键盘出现时
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification*)notify
{

    NSDictionary* info = [notify userInfo];
    //kbSize键盘尺寸 (有width, height)
    self.kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//键盘高度
    NSLog(@"hight_hight:%f",self.kbSize.height);

    CGRect currentFrame = self.view.frame;
    CGFloat change =self.kbSize.height;
    currentFrame.origin.y = currentFrame.origin.y - change ;

    [UIView animateWithDuration:0.00000000001 animations:^{
        self.view.frame = currentFrame;
    }];
}

///键盘消失事件
- (void) keyboardWillHidden {
    
    //视图下沉恢复原状
    [UIView animateWithDuration:0.00000000001 animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
    
    [【你的TextFiled】 resignFirstResponder];

}

// 视图将要消失时,移除通知中心
-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1990 年8 月8 日发布1991 年4 月9 日第一次修订1998 年8 月20 日第二次修订2007 年4 ...
    littlelan阅读 7,623评论 0 4
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,558评论 25 708
  • 2017-6-29 无所事事… 已经进化到可以当着老师和主任的面脱白大褂走人了🤦‍♂️
    Kanyu_阅读 213评论 0 0
  • 模版方法模式 模版方法是一种只需使用继承就可以实现的非常简单的模式模版方法模式由两部分结构组成,第一部分是抽象父类...
    Binaryify阅读 936评论 0 0
  • 时间以“飞”的方式前行,已不太灵活的腿,简直有点跟不上。镜子里的女人容颜也不甚苍老,只是发际的白出卖了她的年纪。 ...
    安之若零阅读 239评论 1 0