弹出键盘时遮挡输入框的问题解决

在进行输入框的动作时,经常会弹出键盘时,遮挡住输入框,所以特意去搜了一下,发现是一个动画的效果 下面增上代码

添加两个输入框,随后 设置代理

在 didbeginediting中 加入代码

 func textFieldDidBeginEditing(textField: UITextField) {
        let animationDuration = NSTimeInterval(0.3)
        UIView.beginAnimations("ResizeForKeyBoard", context: nil)
        UIView.setAnimationDuration(animationDuration)
        //上移单位
        let rect = CGRectMake(0, -130, width, height)
        self.view.frame = rect
        UIView.commitAnimations()
    }

随后再点击屏幕时,屏幕下滑,因此再设置一个动画,将view设置为uicontrol,随后关联事件touch down

 let animationDuration = NSTimeInterval(0.3)
        UIView.beginAnimations("ResizeForKeyBoard", context: nil)
        UIView.setAnimationDuration(animationDuration)
        //上移单位
        let rect = CGRectMake(0, -130, width, height)
        self.view.frame = rect
        UIView.commitAnimations()

整体的代码如下

class ViewController: UIViewController,UITextFieldDelegate {
let animationDuration = NSTimeInterval(0.3)
    var width = CGFloat()
    var height = CGFloat()
   @IBOutlet weak var MiMa: UITextField!
    @IBOutlet weak var zhangHao: UITextField!
    override func viewDidLoad() {
        self.width = self.view.frame.size.width
        self.height = self.view.frame.size.height

        super.viewDidLoad()
        //状态栏的白色字体
        self.zhangHao.delegate = self
        self.MiMa.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }
//进行编辑时 输入框向上移
    @IBAction func click() {
        zhangHao.resignFirstResponder()
        MiMa.resignFirstResponder()
        UIView.beginAnimations("ResizeForKeyBoard", context: nil)
        UIView.setAnimationDuration(animationDuration)
        let rect = CGRectMake(0, 0, width, height)
        self.view.frame = rect
        UIView.commitAnimations()
    }
    func textFieldDidBeginEditing(textField: UITextField) {
        let animationDuration = NSTimeInterval(0.3)
        UIView.beginAnimations("ResizeForKeyBoard", context: nil)
        UIView.setAnimationDuration(animationDuration)
        //上移单位
        let rect = CGRectMake(0, -130, width, height)
        self.view.frame = rect
        UIView.commitAnimations()
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值