Uitextfield funktioniert nicht auf UIScrollView

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Uitextfield funktioniert nicht auf UIScrollView

    Hallo zusammen und ein frohes neues Jahr !
    Folgendes Problem bei einem UIScrollView reagiert nicht der touch von meinem Textfield bei einem UIViewController ist das kein Problem ?(
    komme grad echt nicht mehr weiter ||





    import UIKit

    class NewDoerViewController: UIViewController {
    var scrollView = UIScrollView()
    var contentView = UIView()

    override func viewDidLoad() {
    super.viewDidLoad()
    self.scrollView = UIScrollView()
    self.contentView = UIView()
    let navBackgroundImage:UIImage! = UIImage(named: "Bar")
    self.navigationController?.navigationBar.setBackgroundImage(navBackgroundImage,
    for: .default)
    navigationItem.titleView = UIImageView(image: UIImage(named: "Center_little"))
    scrollView.backgroundColor = .white
    self.scrollView.delegate = self as? UIScrollViewDelegate
    self.contentView.isUserInteractionEnabled = true

    setupButton()
    setupScrollView()
    setupViews()
    }
    func setupScrollView() {
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    contentView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(scrollView)
    self.scrollView.addSubview(contentView)
    //x,w,t,b
    scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    //x,w,t,b
    contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
    contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    }
    func setupViews() {
    contentView.addSubview(firstTextfield)
    firstTextfield.anchor(top: contentView.topAnchor, left: contentView.leftAnchor, bottom: nil, right: contentView.rightAnchor, paddingTop: 16, paddingLeft: 16, paddingBottom: 0, paddingRight: 16, width: 343, height: 50)
    }

    func setupButton() {
    let settingsButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(goBack))
    self.navigationItem.leftBarButtonItem = settingsButton
    }
    let firstTextfield: UITextField = {
    let tf = UITextField()
    tf.placeholder = "Beruf"
    tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
    tf.borderStyle = .roundedRect
    tf.font = UIFont.systemFont(ofSize: 14)
    return tf
    }()
    @objc func goBack() {
    dismiss(animated: true, completion: nil)
    }
    }
  • Gibt dem ScrollView und dem ContentView mal verschiedene Hintergrundfarben, z,B, rot und blau. Dann schaue mal wie groß diese Views zur Laufzeit sind. ;)

    Auch interessant ist das Setzen von clipsToBounds auf true beim contentView. Ich tippe mal, dass dann das TextField nicht mehr zu sehen ist.
  • Dann setzte mal beim contentView clipsToBounds auf true und schon ist das TextField weg. :P

    Die Lösung: Dein contentView ist zu klein. Touch Events werden nur innerhalb des Views verarbeitet. Warum ist das TextField dann sichtbar, obwohl es sich ausserhalb des Views befindet? Weil clipsToBounds den Default Wert false hat und somit auch Subviews angezeigt werden, die sich "ausserhalb" des Views befinden. Allerdings lassen sich diese "ausserhalb" des Views nicht bedienen. ;)
  • Danke bis hier hin für die schnelle Antwort

    Nur wir kriege ich die contentView größer? die farbe ist einfach nicht zu sehen ?(
    Habe es so versucht


    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.frame = self.view.bounds
    contentView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.height, height: scrollView.frame.width)
    }
    func setupScrollView() {
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    contentView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(scrollView)
    self.scrollView.addSubview(contentView)
    //x,w,t,b
    scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    //x,w,t,b
    contentView.anchor(top: scrollView.topAnchor, left: scrollView.leftAnchor, bottom: scrollView.bottomAnchor, right: scrollView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    }

    Hier die extension

    func anchor(top: NSLayoutYAxisAnchor?, left:NSLayoutXAxisAnchor?, bottom:NSLayoutYAxisAnchor?,right:NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat){
    self.translatesAutoresizingMaskIntoConstraints = false
    if let top = top {
    self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
    }
    if let left = left {
    self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
    }
    if let bottom = bottom {
    self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
    }
    if let right = right {
    self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
    }
    if width != 0 {
    self.widthAnchor.constraint(equalToConstant: width).isActive = true
    }
    if height != 0{
    self.heightAnchor.constraint(equalToConstant: height).isActive = true
    }
    }
    }