ios - 键盘向上移动时如何固定UIView的位置?

标签 ios swift uiview keyboard uitextfield

当键盘向上移动时,如何固定UIViewimagelabel的位置?

我编写了一个代码,用于在键盘显示时向上移动我的 textField,但是这会移动所有内容。

func textFieldDidEndEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false)
}


func textFieldDidBeginEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true)
}


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

    return true
}


func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){
    let moveDuration = 0.1
    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)
    UIView.commitAnimations()

}

我该如何解决这个问题?

最佳答案

因为您移动整个 View ,而不是仅移动 TextView 。换行:

self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)

与:

textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)

关于ios - 键盘向上移动时如何固定UIView的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48214948/

相关文章:

ios - 将数轴移动到另一个 View

ios - UIImage获取CVPixelBuffer去除Alpha

ios - 顺利展开和折叠iOS App中的 View ?

ios - 如何在 Xcode 4.3.1 中使用 iOS 4.0 模拟器?

ios - 显示太多 map 标记

ios 8 快速双分页,如 facebook paper 应用程序

iOS - 单例 UIView? Facebook 风格的导航

iphone - 是否有必要在 UIView 动画的完成处理程序中测试 BOOL 完成?

ios - 您如何以编程方式检索iOS照片流中对照片发表的评论?

ios - 如何保留在完成 block 中设置的局部变量?