swifttextfield代理方法

时间:2025-01-27 21:06:56

//MARK:textfield delegate

//键盘的高度

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

//        print("开始编辑")

let fixedTop:CGFloat = kscreenHeight - 252

let convertRect:CGRect = loginScrollView.convertRect(textField.frame, toView: textField.superview)

let textFieledTop = convertRect.origin.y + textField.tt_height

var dispersion:CGFloat = 0.0

let offsetY:CGFloat = loginScrollView.contentOffset.y

if textFieledTop > fixedTop {

dispersion = textFieledTop - fixedTop - offsetY

var offset:CGPoint = loginScrollView.contentOffset

offset.y += dispersion

UIView.beginAnimations(nil, context: nil)

UIView.setAnimationDuration(0.3)

loginScrollView.contentOffset = offset

UIView.commitAnimations()

}

return true

}

func textFieldShouldReturn(textField: UITextField) -> Bool {

//        print("return")

if textField.isEqual(mailAccountTF) {

mailAccountTF.resignFirstResponder()

passwordTF.becomeFirstResponder()

}else if textField.isEqual(passwordTF){

hideKeyboardAction()//键盘退出

}

return true

}

func textFieldDidBeginEditing(textField: UITextField) {

//         print("进入编辑")

if textField.isEqual(mailAccountTF) {

mailAccountTF.becomeFirstResponder()

}else if textField.isEqual(passwordTF){

passwordTF.becomeFirstResponder()

}

}

func textFieldDidEndEditing(textField: UITextField) {

//         print("退出编辑")

hideKeyboardAction()//键盘退出

}

func textFieldShouldEndEditing(textField: UITextField) -> Bool {

//      print("开始退出编辑")

return true

}

//限制输入空格 键盘上的x键,输入一个删除一个

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

if string == " " {

return false

}

return true

}

func textFieldShouldClear(textField: UITextField) -> Bool {

return true

}

//MARK:scrollviewdelegate

func scrollViewWillBeginDragging(scrollView: UIScrollView) {

hideKeyboardAction() //通过滑动隐藏键盘

}

func hideKeyboardAction(){

mailAccountTF.resignFirstResponder()

passwordTF.resignFirstResponder()

}