简体   繁体   English

尽管添加了自动布局约束,UIScrollView也无法滚动

[英]UIScrollView not scrolling despite adding Auto Layout constraints

I'm working on a project where I've added multiple UITextFields to a UIView, which is acting as a container view. 我正在一个项目中,我已经向一个UIView中添加了多个UITextFields,该UIView充当了容器视图。 I then add this container view to a UIScrollView, which in turn, I add to the main view of my screen. 然后,我将此容器视图添加到UIScrollView中,然后将其添加到屏幕的主视图中。 The problem is, my container view appears on the screen, but does not scroll, despite being attached to a UIScrollView, and I can't tell why. 问题是,尽管已附加到UIScrollView,但我的容器视图显示在屏幕上,但是却没有滚动,我不知道为什么。 All of the code, and constraints are in Swift. 所有代码和约束都在Swift中。

Below is the relevant code I have thus far: 以下是我到目前为止的相关代码:

private func setupViews() {
        view.backgroundColor = .nzRedColor()

        registerView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
        registerView.backgroundColor = UIColor.green

        registerScrollView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 300.0)
        registerScrollView.backgroundColor = UIColor.blue


        let addViewsClosure = { (superview: UIView, views: UIView...)->() in
            for view in views {
                view.translatesAutoresizingMaskIntoConstraints = false
                if ((view as? UIImageView) != nil) {
                    superview.addSubview(view)
                } else if ((view as? UITextField) != nil) {
                    self.registerView.addSubview(view)
                    self.registerScrollView.addSubview(self.registerView)
                    superview.addSubview(self.registerScrollView)
                } else {
                    superview.addSubview(view)
                }
            }
        }

        let customizeTextFields = { (textfields: UITextField...) in
            for textfield in textfields {
                textfield.backgroundColor = UIColor(white: 1.0, alpha: 0.3)
                textfield.font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightRegular)
                textfield.layer.cornerRadius = 2.0
            }
        }

        customizeTextFields(firstNameTextField, lastNameTextField, cityTextField, countryTextField, emailTextField, passwordTextField, passwordConfirmationTextField)


        addViewsClosure(view, logoImageView, firstNameTextField, lastNameTextField, cityTextField, countryTextField, emailTextField, passwordTextField, passwordConfirmationTextField, registerButton, skipButton)


        logoImageView.constrainTop(at: 50, after: topLayoutGuide.anchorableRepresentation)
        logoImageView.constrainCenterHorizontally()
        logoImageWidthConstraint = logoImageView.constrainWidth(at: 0)
        logoImageHeightConstraint = logoImageView.constrainHeight(at: 0)
        updateLogoImageSize()


        firstNameTextField.constrainTop(at: 10, after: logoImageView)
        firstNameTextField.constrainHeight(at: 50)
        firstNameTextField.constrainLeading(alignedWith: view, offset: 10)
        firstNameTextField.constrainTrailing(alignedWith: view, offset: -10)

        lastNameTextField.constrainTop(at: 10, after: firstNameTextField)
        lastNameTextField.constrainHeight(at: 50)
        lastNameTextField.constrainLeading(alignedWith: firstNameTextField)
        lastNameTextField.constrainTrailing(alignedWith: firstNameTextField)

        cityTextField.constrainTop(at: 10, after: lastNameTextField)
        cityTextField.constrainHeight(at: 50)
        cityTextField.constrainLeading(alignedWith: firstNameTextField)
        cityTextField.constrainTrailing(alignedWith: firstNameTextField)

        countryTextField.constrainTop(at: 10, after: cityTextField)
        countryTextField.constrainHeight(at: 50)
        countryTextField.constrainLeading(alignedWith: firstNameTextField)
        countryTextField.constrainTrailing(alignedWith: firstNameTextField)

        emailTextField.constrainTop(at: 10, after: countryTextField)
        emailTextField.constrainHeight(at: 50)
        emailTextField.constrainLeading(alignedWith: firstNameTextField)
        emailTextField.constrainTrailing(alignedWith: firstNameTextField)

        passwordTextField.constrainTop(at: 10, after: emailTextField)
        passwordTextField.constrainHeight(at: 50)
        passwordTextField.constrainLeading(alignedWith: firstNameTextField)
        passwordTextField.constrainTrailing(alignedWith: firstNameTextField)

        passwordConfirmationTextField.constrainTop(at: 10, after: passwordTextField)
        passwordConfirmationTextField.constrainHeight(at: 50)
        passwordConfirmationTextField.constrainLeading(alignedWith: firstNameTextField)
        passwordConfirmationTextField.constrainTrailing(alignedWith: firstNameTextField)

        registerView.constrainTop(at: 0)
        registerView.constrainHeight(at: self.view.frame.height)
        registerView.constrainLeading(at: 0)
        registerView.constrainTrailing(at: 0)

        registerScrollView.constrainTop(at: 50, after: logoImageView)
        registerScrollView.constrainHeight(at: 300.0)
        registerScrollView.constrainLeading(at: 0)
        registerScrollView.constrainTrailing(at: 0)

        registerButton.constrainTop(at: 30, after: passwordConfirmationTextField)
        registerButton.constrainHeight(at: 50)
        registerButton.constrainLeading(alignedWith: firstNameTextField)
        registerButton.constrainTrailing(alignedWith: firstNameTextField)

        skipButton <- { $0.top >= registerButton.anchors.bottom + 12 }
        skipButton.constrainBottom(at: 12)
        skipButton.constrainCenterHorizontally()
    }

Two things I wish to note here: 我想在这里注意两件事:

  1. Despite me making the registerView's background colour to be "Green", I don't the view appearing. 尽管我将registerView的背景色设置为“绿色”,但没有显示该视图。

  2. For some reason, all of the UITextFields appear directly on the UIScrollView (because its colour is blue), and the UIScrollView is visible, but unfortunately not scrollable. 由于某些原因,所有UITextField都直接显示在UIScrollView上(因为其颜色为蓝色),并且UIScrollView是可见的,但不幸的是无法滚动。 Can anyone see what it is I'm doing wrong? 有人可以看到我在做什么错吗?

When using auto-layout with scroll views, the content of the scroll view defines the .contentSize . 在滚动视图中使用自动布局时,滚动视图的内容定义.contentSize That means your registerView must have all its constraints set properly. 这意味着您的registerView 必须正确设置所有约束。

You set the registerView background to Green, but you don't see the view? 您将registerView背景设置为绿色,但是看不到视图? I bet if you set its .clipsToBounds to true you won't see the text fields anymore either. 我敢打赌,如果将其.clipsToBounds设置为true您也将不会再看到文本字段。

    registerView.constrainTop(at: 0)
    registerView.constrainHeight(at: self.view.frame.height)
    registerView.constrainLeading(at: 0)
    registerView.constrainTrailing(at: 0)

You've set Top, Leading, Trailing and Height constraints - but looks like you're missing Width and Bottom. 您已经设置了“上”,“前”,“后”和“高”约束-但看起来好像缺少“宽度”和“下”。

The Bottom constraint should probably be set to 0 - like Top, Leading and Trailing. 底部约束可能应设置为0像顶部,前导和尾随。

I'm guessing you want registerView Height constraint to be determined by the last text field - passwordConfirmationTextField - but if that's the case, you're still missing that one. 我猜想您希望registerView Height约束由最后一个文本字段( passwordConfirmationTextField确定,但是如果是这样,您仍然会丢失该字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM