UIScrollView scrolling when displaying keyboard - Swift

0

I'm using a UIView with a UIScrollView to assemble a layout, but every time the keyboard is shown scrollview and scroll down, I tried some solutions, found here and in other forums, none of them solved my problem, Note: I'm creating the layout programmatically

Follow my code for review:

import UIKit

class ExempleUIViewController: UIViewController, UIScrollViewDelegate, InputTextViewDelegate, SwitchViewDelegate {

  func performSwitch(cell: UIView, value: Bool) { // retorna valores do campo se alterado

  }

  func performInputTextView(cell: UIView, value: Any ? ) { // retorna valores do campo se alterado

  }

  //Campos customizados

  var descriptionView = DescriptionView()

  var inputTextView = InputTextView()

  var inputTextLongView = InputTextLongView()

  var disclousureView = DisclousureView()

  var switchView = SwitchView()

  var inputDateView = InputDateView()

  var inputTextPickerView = InputTextPickerView()

  //UiscrollView

  let scrollView: UIScrollView = {
    let v = UIScrollView()
    v.translatesAutoresizingMaIntoConstraints = false
    v.alwaysBounceVertical = true
    v.showsHorizontalScrollIndicator = false
    v.backgroundColor = UIColor.white
    return v
  }()

  //Variavel para controlle da altura da UIScrollView

  var scrollHeigth = 1000

  //Trava a UIScrollView na horizontal

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
      scrollView.contentOffset.x = 0
    }
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false

    title = "ScrollView"

    scrollView.delegate = self

    //adiciona a Scrollview na view

    self.view.addSubview(scrollView)

    scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

    //seta as views dos campos
    setupViews()

    //Dispara evento quando o teclado e aparece e desaparece para reajustar posicao do scrollview
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

  }

  //tenta tratar o bug que ocorre quando o teclado e mostrado nao funciona

  @objc func keyboardWillShow(notification: NSNotification) {

    var userInfo = notification.userInfo!
      var keyboardFrame: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as!NSValue).cgRectValue
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)
    var contentInset: UIEdgeInsets = self.scrollView.contentInset
    contentInset.bottom = (keyboardFrame.size.height + 40)
    scrollView.contentInset = contentInset
  }

  @objc func keyboardWillHide(notification: NSNotification) {

    let contentInset: UIEdgeInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInset
  }

  //adiciona as views na scrollview programaticamente
  func setupViews() {
    descriptionView.titleLabel.text = "Description"

    scrollView.addSubview(descriptionView)
    descriptionView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16).isActive = true
    descriptionView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    descriptionView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    inputTextView.titleLabel.text = "Input Text"
    inputTextView.textField.placeholder = "Test123"
    inputTextView.delegate = self

    scrollView.addSubview(inputTextView)
    inputTextView.topAnchor.constraint(equalTo: descriptionView.bottomAnchor).isActive = true
    inputTextView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    inputTextView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    inputTextLongView.placeHolder = "TextLongView"
    inputTextLongView.delegate = self

    scrollView.addSubview(inputTextLongView)
    inputTextLongView.topAnchor.constraint(equalTo: inputTextView.bottomAnchor).isActive = true
    inputTextLongView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    inputTextLongView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    disclousureView.titleLabel.text = "DiclousureText"
    disclousureView.subTitleLabel.text = "DiclousureSelect"

    scrollView.addSubview(disclousureView)
    disclousureView.topAnchor.constraint(equalTo: inputTextLongView.bottomAnchor).isActive = true
    disclousureView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    disclousureView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    switchView.titleLabel.text = "SwitchTitle"
    switchView.delegate = self

    scrollView.addSubview(switchView)
    switchView.topAnchor.constraint(equalTo: disclousureView.bottomAnchor).isActive = true
    switchView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    switchView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    inputDateView.titleLabel.text = "Date"
    inputDateView.delegate = self
    inputDateView.textField.dateFormatter.locale = Locale(identifier: "pt-br")
    inputDateView.textField.dateFormatter.dateFormat = "dd/MM/YYYY"
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "pt-br")
    dateFormatter.dateFormat = "dd/MM/yyyy"
    var dateAux = dateFormatter.string(from: Date())
    let date = dateFormatter.date(from: dateAux)
    inputDateView.setDate(date!)

    scrollView.addSubview(inputDateView)
    inputDateView.topAnchor.constraint(equalTo: switchView.bottomAnchor).isActive = true
    inputDateView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    inputDateView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true

    inputTextPickerView.titleLabel.text = "SwitchTitle"
    inputTextPickerView.delegate = self
    inputTextPickerView.strings = ["Teste1", "Teste2", "Teste3", "Teste4", "Teste5"]
    inputTextPickerView.keys = ["0", "1", "2", "3", "4"]

    scrollView.addSubview(inputTextPickerView)
    inputTextPickerView.topAnchor.constraint(equalTo: inputDateView.bottomAnchor).isActive = true
    inputTextPickerView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    inputTextPickerView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
  }

  //seta tamanha na scrollview

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: CGFloat(scrollHeigth), right: screenWidth)
    scrollView.contentSize = CGSize(width: screenWidth, height: CGFloat(scrollHeigth))
  }
}
    
asked by anonymous 17.08.2018 / 18:04

1 answer

3

I resolvo this problem using IQKeyboardManager Very easy to use and you will not have these problems.

    
04.10.2018 / 15:20