如何获取文本视图以触发滚动视图向上移动键盘?

时间:2017-09-29 01:16:10

标签: swift xamarin.ios keyboard textview scrollview

我正在创建一个存在于scrollview中的视图上的表单。它包含两个textField和一个textView。我已经将来自在线资源(like this one)的一些代码拼凑在一起,这样如果视频上的textField太低或被键盘阻挡,它就会向上滚动。问题是它似乎不适用于textViews。

我已尽力理解为什么textViews不会触发相同的行为,但我真的可以使用一些帮助。这是我的VC代码:

import UIKit

class AddAPlaceBasicInfoViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

// Outlets
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var categoryTextField: UITextField!
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(noti:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    // Default to an disabled "Next" button.
    nextButton.isEnabled = false
    nextButton.backgroundColor = UIColor(red: 89/255, green: 89/255, blue: 89/255, alpha: 1/1)

    // Check that fields are filled.
    nameTextField.delegate = self
    descriptionTextView.delegate = self

    // Helper functions.
    createToolbar()
    descriptionTextViewSetup(descriptionTextView)

}

// Creates the done button above the category picker.
func createToolbar() {

    // Creates an instance of UIToolbar() named "toolbar".
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    // Set up button properties.
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(AddAPlaceBasicInfoViewController.dismissKeyboard))

    // Set color to teal.
    doneButton.tintColor = UIColor(red: 0/255, green: 134/255, blue: 111/255, alpha: 1)

    // Set up button in the toolbar and make it interactive.
    toolbar.setItems([doneButton], animated: false)
    toolbar.isUserInteractionEnabled = true

    // Add the toolbar as an accessory view to the category picker.
    nameTextField.inputAccessoryView = toolbar
    categoryTextField.inputAccessoryView = toolbar
    descriptionTextView.inputAccessoryView = toolbar

    // Set toolbar's background to white.
    toolbar.backgroundColor = UIColor.white

}

// Function to dismiss the keyboard. Used when the user taps the "Done" button in the toolbar.
func dismissKeyboard() {

    view.endEditing(true)

    if nameTextField.text?.characters.count == 0 || categoryTextField.text?.characters.count == 0 || descriptionTextView.text == "What's this place like? (You'll be able to add a photo on the next screen)" {

        nextButton.isEnabled = false
        nextButton.backgroundColor = UIColor(red: 89/255, green: 89/255, blue: 89/255, alpha: 1/1)

    } else {

        nextButton.isEnabled = true
        nextButton.backgroundColor = UIColor(red: 0/255, green: 134/255, blue: 111/255, alpha: 1/1)

    }

}

// Function to create placeholder text.

func descriptionTextViewSetup(_ textView: UITextView) {

    // Modifications
    descriptionTextView.text = "What's this place like? (You'll be able to add a photo on the next screen)"
    descriptionTextView.textColor = UIColor(red: 199/255, green: 199/255, blue: 205/255, alpha: 1/1)
    descriptionTextView.textContainer.lineFragmentPadding = 0
    //descriptionTextView.textColor = UIColor.lightGray

}

//---------------------------------
// MARK: - Notification Center
//---------------------------------

func keyboardWillHide(noti: Notification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}


func keyboardWillShow(noti: Notification) {

    guard let userInfo = noti.userInfo else { return }
    guard var keyboardFrame: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    var contentInset:UIEdgeInsets = scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height
    scrollView.contentInset = contentInset
}

}

1 个答案:

答案 0 :(得分:0)

如果您要为可以输入信息的用户创建表单,那么我建议使用静态UITableView。如果您的UIViewController是UITableViewController类型,则可以使用静态UITableView。静态TableView使得放置控件非常容易,因为UITableView已经有一个UIScrollView,它会在UITextView聚焦时自动滚动。

尝试一下:enter image description here