调整键盘ios 8 swift隐藏的文本字段

时间:2015-04-08 12:12:01

标签: ios swift

当uiviewcontroller上有键盘时,任何人都能给我一个swift编程的示例,用于滚动文本字段

当键盘存在时如何使uitextfield向上移动

2 个答案:

答案 0 :(得分:0)

使用此代码

  override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    self.view.endEditing(true)
}
func textFieldDidBeginEditing(textField: UITextField) {



    textField.keyboardType = UIKeyboardType.EmailAddress
    self.animateViewMoving(true, moveValue: 50)

}
func textFieldDidEndEditing(textField: UITextField) {

    self.animateViewMoving(false, moveValue: 50)


}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    return true

}
func animateViewMoving (up:Bool, moveValue :CGFloat){

    var movementDuration:NSTimeInterval = 0.3
    var movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

答案 1 :(得分:0)

使用TPKeyboardAvoiding。只需将Scrollview类设置为TPKeyboardAvoidingScrollView&导入代码中的文件。使用以下链接 https://github.com/michaeltyson/TPKeyboardAvoiding