Swift: Scroll View only when a TextField or Button

2019-06-10 03:46发布

问题:

Below is the code that I'm trying to use to have the view scroll only when the focused textField or UIButton is hidden by the keyboard:

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)
        }
    }
}

Problem: The View scrolls even when the keyboard is not hiding anything.

View at Launch:

View Upon Tapping on a TextField:

Question: How do I check to see if the focused field is hidden by the keyboard, and ONLY if it is, push it above it?

Project: TextFieldScroll

EDIT

App works fine in Portrait mode, however, not in landscape mode. Also in landscape mode, when the Username textField keyboard is up and I tap the Password textField, it remains hidden behind the keyboard. I updated the project on Github and fixed the link. Thanks

Screen shot in landscape:

回答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;
   }


回答2:

I don't see you setting the scrollView's contentSize. Thus, the scrollView scrolls with the keyboard since it uses the screen's size.