Swift: Scroll View only when a TextField or Button is hidden by the Keyboard

筅森魡賤 提交于 2019-12-23 04:33:40

问题


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.



来源:https://stackoverflow.com/questions/32063357/swift-scroll-view-only-when-a-textfield-or-button-is-hidden-by-the-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!