移动键盘后面的内容并不起作用,Swift

时间:2016-03-02 16:12:39

标签: swift uiviewcontroller uiscrollview

在视图控制器中,我有UIImageView和多个uitextfields。我使用以下代码(基于Apple文档)在调用键盘通知时向上移动scrollView,以推高隐藏的文本字段。然而,当键盘出现时,视图确实向上移动。 我在这里错过了什么吗?

class NewViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scrollView: UIScrollView!

var activeTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.contentSize.height = 1000

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func registerForKeyboardNotifications() {
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: "keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
    notificationCenter.addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillBeShown(sender: NSNotification) {
    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets


    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    print(aRect)

    let activeTextFieldRect: CGRect? = activeTextField?.frame
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
        scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
    }
}


func keyboardWillBeHidden(sender: NSNotification) {
    let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

func textFieldDidBeginEditing(textField: UITextField) {
    activeTextField = textField
}

func textFieldDidEndEditing(textField: UITextField) {
    activeTextField = nil
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.registerForKeyboardNotifications()
}

override func viewDidDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

1 个答案:

答案 0 :(得分:0)

尝试此代码并根据您的情况设置“moveValue”。 记得扩展UITextFieldDelegate

 func textFieldDidBeginEditing(textField: UITextField)
{
    self.animateViewMoving(true, moveValue: 85)
}
func textFieldDidEndEditing(textField: UITextField)
{
    self.animateViewMoving(false, moveValue: 85)
}
func animateViewMoving (up:Bool, moveValue :CGFloat)
{
    let durataMovimento:NSTimeInterval = 0.3
    let movimento:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(durataMovimento )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movimento)
    UIView.commitAnimations()
}