2017-09-29 29 views
0

我正在创建一个生活在scrollview视图中的窗体。它包含两个textField和一个textView。我已经拼凑了一些来自在线资源的代码(like this one),以便在textField在视图上太低或被键盘阻挡时滚动。问题是它似乎不适用于textViews。如何获取textview以触发滚动视图向上移动键盘?

我已经尽力理解为什么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 
} 

} 
+0

您是否检查了此答案? [使用Swift键盘移动视图](https://stackoverflow.com/questions/26070242/move-view-with-keyboard-using-swift) –

回答

0

如果你正在为用户创建一个表单,他们可以输入信息,那么我会建议使用静态UITableView。如果您的UIViewController是UITableViewController类型,则可以使用静态UITableView。静态TableView使得放置控件变得非常简单,并且由于UITableView已经有了一个UIScrollView,它在UITextView处于焦点时自动滚动。

试一试:enter image description here

相关问题