创建可滚动的 UIView 时遇到问题(swift + Interface Builder)

trouble creating a scrollable UIView (swift + Interface Builder)

请不要立即怀疑这是重复的,我已经在 Whosebug 上使用了大约 2 天,现在尝试了各种方法来做到这一点,阅读了很多博客文章(可能已经过时了)但仍然没有运气.

基本上,我有一个自定义 UIView,它用于绘制很多圆圈(这只是起点),我无法让视图向下滚动到初始加载时可见圆圈以外的那些圆圈。我为其中的大约 200 个创建了 for 循环,以确保有可以滚动到的内容。

这是我的查看代码:

import UIKit

class Draw2D: UIView {

     required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);

    }

    override func drawRect(rect: CGRect) {

        let context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        let colorSpace = CGColorSpaceCreateDeviceRGB();
        let components: [CGFloat] = [0.0, 0.0, 1.0, 1.0];
        let color = CGColorCreate(colorSpace, components);

        CGContextSetStrokeColorWithColor(context, color);

        var ctr = 0;
        var ctr2 = 0;
        for(var i:Int = 1; i<200; i++){

            var ii: CGFloat=CGFloat(10+ctr);
            var iii: CGFloat = CGFloat(30+ctr2);
       CGContextStrokeEllipseInRect(context, CGRectMake(ii, iii, 33, 33));

            if(ctr<200)
            {
            ctr+=160;
            }else{
                ctr=0;
                ctr2+=50;
            }
        }

        CGContextStrokePath(context);

    }
}

在界面生成器中,我将视图 class 设置为 Draw2D,将控制器设置为我的自定义 ViewController class。视图控制器 class 目前是标准的,没有添加额外的选项。

我尝试在屏幕上拖动一个滚动视图,我尝试删除标准视图,添加滚动视图,然后将我的 Draw2D 视图放在上面,但没有成功。我尝试更改滚动视图的大小以使其小于我的内容 (draw2d) 视图但什么也没有。我尝试取消选中自动布局,并根据我在网上找到的某些教程将模拟大小更改为自由格式。

我也曾尝试在 UIView 中以编程方式创建滚动视图并将其添加为子视图,但这没有帮助,也许我做错了,我不知道,但我真的 运行 没有选择和来源来产生想法。

首先,可以直接继承UIScrollView:

class Draw2D: UIScrollView {

接下来,您需要将UIScrollView 的内容大小设置为您插入的元素的高度。例如,假设您有 4 个圈子:

let radius: CGFloat = 200
var contentHeight: CGFloat = 0

for i in 0...4 {
    // initialize circle here
    var circle = UIView()

    // customize the view
    circle.layer.cornerRadius = radius/2
    circle.backgroundColor = UIColor.redColor()

    // set the frame of the circle    
    circle.frame = CGRect(x: 0, y: CGFloat(i)*radius, width: radius, height: radius)

    // add to scroll view
    self.addSubview(circle)

    // keep track of the height of the content
    contentHeight += radius
}

此时你有 4 个圆圈,一个接一个,总高度为 800 像素。因此,您可以按如下方式设置滚动视图的内容大小:

self.contentSize = CGSize(width: self.frame.width, height: contentHeight)

只要 contentHeight > 设备屏幕高度,您的视图就会滚动。

下面是完整的工作代码:

import UIKit
import Foundation

class Draw2D: UIScrollView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        let radius: CGFloat = 200
        var contentHeight: CGFloat = 0

        for i in 0...4 {
            // initialize circle here
            var circle = UIView()

            // customize the view
            circle.layer.cornerRadius = radius/2
            circle.backgroundColor = UIColor.redColor()

            // set the frame of the circle
            circle.frame = CGRect(x: 0, y: CGFloat(i)*radius, width: radius, height: radius)

            // add to scroll view
            self.addSubview(circle)

            // keep track of the height of the content
            contentHeight += radius
        }

        self.contentSize = CGSize(width: self.frame.width, height: contentHeight)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}