【问题标题】:how to fix this function to move textfield when keyboard is presented in scrollview当键盘出现在滚动视图中时,如何修复此功能以移动文本字段
【发布时间】:2018-11-03 08:18:35
【问题描述】:

所以我有这个功能可以在ScrollView 中移动TextField,但问题是顶部的一些TextField 在出现keyboard 时有时会移动得太远

这是我的代码:

 func textFieldDidBeginEditing(_ textField: UITextField) {
        moveTextFIeld(textField: textField, moveDistance: -20, up: true)
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        moveTextFIeld(textField: textField, moveDistance: -20, up: false)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {


        tanggalLahirTextField.resignFirstResponder()
}

    func moveTextFIeld(textField:UITextField,moveDistance:Int,up:Bool){
        let moveDuration = 0.3
        let movement : CGFloat = CGFloat(up ? moveDistance : -moveDistance)
        UIView.beginAnimations("animateTextField", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(moveDuration)
        self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
//        self.view.frame=CGRect.offsetBy(self.view.frame,0,movement)
        UIView.commitAnimations()


    }

【问题讨论】:

标签: swift


【解决方案1】:

我们可以使用 cocoapods 中的 IQKeyboardManager。Click here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2014-10-30
    • 2018-12-26
    • 1970-01-01
    • 2011-08-23
    • 2020-05-21
    • 2014-01-21
    相关资源
    最近更新 更多