0

Ola estou usando uma UIView com uma UIScrollView para montar um layout, mas toda vez que o teclado e mostrado a scrollview e rolada para baixo, ja tentei algumas soluções, encontradas por aqui e em outros fóruns mais nenhuma resolveu meu problema, Obs: Estou criando o layout programaticamente

segue meu código para analise :

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

1
  • Achei uma solução aparentemente apenas setar o bottomAnchor da ultima view no bottomAnchor da scrollview e utilizar a solução que ja estou usando para ajustar a posicao da scrollview de acordo com o textfield selecionado Commented 17/08/2018 às 19:54

1 Resposta 1

3

Eu resolvo esse problema utilizando IQKeyboardManager Muito tranquilo de utilizar e você não terá esses problemas.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .