码迷,mamicode.com
首页 > 编程语言 > 详细

swift 点击Textfield 后自动上移,避免键盘被遮住

时间:2017-07-25 12:39:19      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:current   uiview   避免   bool   键盘   style   elf   UI   duration   

一些textfield在键盘显示出来的时候会被挡住,所以在编辑textfield我们可以把视图上移

方法是给textfield增加两个监听事件

    @IBAction func beginEdit(_ sender: UITextField) {
        animateViewMoving(up: true, moveValue: 100)
    }
        
    @IBAction func endEdit(_ sender: UITextField) {
        animateViewMoving(up: false, moveValue: 100)
    }
    
    func animateViewMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:TimeInterval = 0.3
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.view.frame = self.view.frame.offsetBy(dx: 0,  dy: movement)
        UIView.commitAnimations()
    }

 

swift 点击Textfield 后自动上移,避免键盘被遮住

标签:current   uiview   避免   bool   键盘   style   elf   UI   duration   

原文地址:http://www.cnblogs.com/zrcx/p/7233295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!