zoukankan      html  css  js  c++  java
  • 键盘弹起

    - (void)viewWillAppear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    // 键盘显示
    - (void)keyboardWillShow:(NSNotification *)notification
    {
        CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat height = keyboardFrame.origin.y;
        
        // 计算视图需要移动的距离
        CGFloat space = self.imageView.frame.origin.y + self.imageView.frame.size.height;
        
        // 得出键盘距离输入框的间距
        CGFloat trsformY = height - space;
        
        if (trsformY < 0) {
            CGRect frame = self.view.frame;
            frame.origin.y = trsformY;
            self.view.frame = frame;
        }
        
    }
    
    // 键盘隐藏
    - (void)keyboardWillHide:(NSNotification *)notification
    {
        CGRect frame = self.view.frame;
        frame.origin.y = 64;
        self.view.frame = frame;
    }
  • 相关阅读:
    【笔记】Maven使用入门
    【笔记】c++文件
    【笔记】IntelliJ IDEA配置Hibernate
    【HTML5校企公益课】第四天
    【c++习题】【17/4/16】动态分配内存
    C#
    C#
    C#
    C#
    C#
  • 原文地址:https://www.cnblogs.com/menglingxu/p/6118711.html
Copyright © 2011-2022 走看看