没有 Interface Builder 的 UIScrollView 和 Autolayout - 滚动视图如何获得它的高度?

Posted

技术标签:

【中文标题】没有 Interface Builder 的 UIScrollView 和 Autolayout - 滚动视图如何获得它的高度?【英文标题】:UIScrollView and Autolayout without Interface Builder - how does the scroll view get its height? 【发布时间】:2015-04-21 19:50:52 【问题描述】:

我正在掌握 Xcode 6 和 ios 自动布局/约束没有使用界面生成器/故事板,但目前正在努力处理滚动视图。

我编写了以下代码(在我学习的时候都是手写的!)它会在屏幕上放置一个滚动视图,其中包含一个内容视图,而内容视图又包含几个额外的视图。

当我滚动时,滚动事件触发良好。我遇到的问题是,当我放手时,滚动视图只是弹回到起点,这表明它没有接收到内容高度。我认为。或者,也许我在某个地方搞砸了约束?

谁能给我一些关于我可能在哪里出错的指示?

谢谢。

class TestScrollController: UIViewController, UIScrollViewDelegate 


    var scrollView: UIScrollView!
    var contentView = UIView()
    var blueView = UIView()
    var orangeView = UIView()



    override func viewDidLoad() 
        super.viewDidLoad()

        var superView = self.view

        var scrollView = UIScrollView()
        scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
        scrollView.scrollEnabled = true
        scrollView.alwaysBounceVertical = true
        scrollView.backgroundColor = UIColor.brownColor()
        scrollView.delegate = self
        superView.addSubview(scrollView);

        var contentView = UIView()
        contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.backgroundColor = UIColor.greenColor()
        scrollView.addSubview(contentView)

        let blueView = UIView()
        blueView.setTranslatesAutoresizingMaskIntoConstraints(false)
        blueView.backgroundColor = UIColor.blueColor()
        contentView.addSubview(blueView);

        let orangeView = UIView()
        orangeView.setTranslatesAutoresizingMaskIntoConstraints(false)
        orangeView.backgroundColor = UIColor.orangeColor()
        contentView.addSubview(orangeView);




        //scrollview Constraints
        let scrollViewConstraintTop = NSLayoutConstraint(
            item: scrollView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Top,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(scrollViewConstraintTop)

        let scrollViewConstraintRight = NSLayoutConstraint(
            item: scrollView,
            attribute: .Trailing,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Right,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(scrollViewConstraintRight)

        let scrollViewConstraintBottom = NSLayoutConstraint(
            item: scrollView,
            attribute: .Bottom,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(scrollViewConstraintBottom)

        let scrollViewConstraintLeft = NSLayoutConstraint(
            item: scrollView,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Left,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(scrollViewConstraintLeft)



        //contentView Constraints
        let contentViewConstraintTop = NSLayoutConstraint(
            item: contentView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: scrollView,
            attribute: .Top,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(contentViewConstraintTop)

        let contentViewConstraintRight = NSLayoutConstraint(
            item: contentView,
            attribute: .Trailing,
            relatedBy: .Equal,
            toItem: scrollView,
            attribute: .Right,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(contentViewConstraintRight)

        let contentViewConstraintBottom = NSLayoutConstraint(
            item: contentView,
            attribute: .Bottom,
            relatedBy: .Equal,
            toItem: scrollView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(contentViewConstraintBottom)

        let contentViewConstraintLeft = NSLayoutConstraint(
            item: contentView,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: scrollView,
            attribute: .Left,
            multiplier: 1.0,
            constant: 0
        )
        superView.addConstraint(contentViewConstraintLeft)



        //blueView Constraints
        let blueViewConstraintHeight = NSLayoutConstraint(
            item: blueView,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100.0
        )
        superView.addConstraint(blueViewConstraintHeight)

        let blueViewConstraintTop = NSLayoutConstraint(
            item: blueView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: contentView,
            attribute: .Top,
            multiplier: 1.0,
            constant: 50.0
        )
        superView.addConstraint(blueViewConstraintTop)

        let blueViewConstraintLeft = NSLayoutConstraint(
            item: blueView,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Left,
            multiplier: 1.0,
            constant: 50.0
        )
        superView.addConstraint(blueViewConstraintLeft)

        let blueViewConstraintRight = NSLayoutConstraint(
            item: blueView,
            attribute: .Trailing,
            relatedBy: .Equal,
            toItem: superView,
            attribute: .Right,
            multiplier: 1.0,
            constant: -50.0
        )
        superView.addConstraint(blueViewConstraintRight)



        //orangeView Constraints
        let orangeViewConstraintWidth = NSLayoutConstraint(
            item: orangeView,
            attribute: .Width,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100.0
        )
        superView.addConstraint(orangeViewConstraintWidth)

        let orangeViewConstraintHeight = NSLayoutConstraint(
            item: orangeView,
            attribute: .Height,
            relatedBy: .Equal,
            toItem: nil,
            attribute: .NotAnAttribute,
            multiplier: 1.0,
            constant: 100.0
        )
        superView.addConstraint(orangeViewConstraintHeight)


        let orangeViewConstraintTop = NSLayoutConstraint(
            item: orangeView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: blueView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 400.0
        )
        superView.addConstraint(orangeViewConstraintTop)

        let orangeViewConstraintBottom = NSLayoutConstraint(
            item: orangeView,
            attribute: .Bottom,
            relatedBy: .Equal,
            toItem: contentView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 400.0
        )
        superView.addConstraint(orangeViewConstraintBottom)


        let orangeViewConstraintCenterX = NSLayoutConstraint(
            item: orangeView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: blueView,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0.0
        )
        superView.addConstraint(orangeViewConstraintCenterX)


    




    func scrollViewDidScroll(scrollView: UIScrollView) 
        println("scrollViewDidScroll")
    





【问题讨论】:

你可以在这里找到一个很好的解释(在 obj-c 中):***.com/a/24972208/2477632 帮助很大,谢谢! 【参考方案1】:

您可以使用.contentsize(CGRECT) 来设置内容的大小inside UIScrollView 由于您没有设置contentSize,因此您无法滚动到任何内容

【讨论】:

以上是关于没有 Interface Builder 的 UIScrollView 和 Autolayout - 滚动视图如何获得它的高度?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Interface Builder 在其他 UI 元素的背景中添加 UIButton?

如何指定我希望 UI 元素在 Interface Builder 中进行方向更改的位置?

如何使用主 iPhone 视图之外的 UI 元素在 Interface Builder 中设置 UIScrollView?

代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧

在我看来,iOS 项目未显示从 Interface Builder 构建的 Interface 元素

在 Interface Builder 中更新 UIView 类