Swift:仅在键盘隐藏TextField或Button时滚动查看

时间:2015-08-18 04:04:54

标签: swift ios8 scrollview uikeyboard

下面是我尝试使用的代码,只有在键盘隐藏了聚焦的textField或UIButton时才能滚动视图:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var buttonToBottomConstraint: NSLayoutConstraint!

    @IBOutlet weak var textFieldUsername: UITextField!
    @IBOutlet weak var textFieldPassword: UITextField!
    @IBOutlet weak var scrollView: UIScrollView!


    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
    }

    func textFieldDidBeginEditing(textField: UITextField) {
    }

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

        return true
    }

    func keyboardWillBeHidden(notification: NSNotification){

        self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
    }

    func keyboardWillBeShown(notification: NSNotification){

        let userInfo = notification.userInfo!

        if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size{

             self.scrollView .setContentOffset(CGPointMake(0, kbsize.or), animated: true)
        }
    }
}

问题:即使键盘没有隐藏任何内容,View也会滚动。

启动时查看:

enter image description here

点击TextField时查看:

enter image description here

问题:如何检查键盘是否隐藏了聚焦区域,如果是,则将其推到上方?

项目:TextFieldScroll

修改

应用程序在纵向模式下工作正常,但不适用于横向模式。同样在横向模式下,当用户名textField键盘启动并点击密码textField时,它仍然隐藏在键盘后面。我在Github上更新了项目并修复了链接。感谢

横向拍摄的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

var currentTextField:UITextField?

@IBOutlet weak var textFieldUsername: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
@IBOutlet weak var scrollView: UIScrollView!


override func viewDidLoad() 
{
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
}

func textFieldDidBeginEditing(textField: UITextField)
{
currentTextField=textField;
}

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

    return true
}

func keyboardWillBeHidden(notification: NSNotification){
      currentTextField=nil;
    self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}

func keyboardWillBeShown(notification: NSNotification){

    let userInfo = notification.userInfo!

    if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size
    {
          let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbsize.height, right: 0)
        let screenSize: CGRect = UIScreen.mainScreen().bounds
       let screenHeight = screenSize.height
        let height:CGFloat=screenHeight-(kbsize.height)-(currentTextField!.frame.size.width)

         if(currentTextField!.frame.origin.y>=height )
        {
            var scrollPoint: CGPoint  = CGPointMake(0.0, currentTextField!.frame.origin.y - (kbsize.height));
            self.scrollView .setContentOffset(scrollPoint, animated: true)
        }

    }
}

  func textFieldDidEndEditing(textField: UITextField) {
     currentTextField=nil;
   }

答案 1 :(得分:0)

我没有看到您设置scrollView的{​​{1}}。因此,contentSize使用键盘滚动,因为它使用屏幕的大小。