UIscrollview不能快速滚动

时间:2016-03-27 00:22:07

标签: ios swift uiview uiscrollview

我有以下问题,我在view2上面创建了view1

然后将scrollview添加到view1,scrollview包含一些由代码添加的ui元素

我的滚动视图没有滚动的问题,它的内容显示在视图2上方,而我需要它在它后面并且能够滚动它

截图正在关注

enter image description here

我的添加UI元素的代码如下:

        mainScrollView = UIScrollView(frame: CGRectMake(0, 0, self.view.frame.size.width, 1000))

        let awesomeView2: UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.size.width, 180))
        awesomeView2.load(selectedItem.listingImage);

        mainScrollView.addSubview(awesomeView2);

        let title: UILabel = UILabel(frame: CGRectMake(0, 180, self.view.frame.size.width, 44));
        title.text = selectedItem.listingTitle;
        title.textAlignment = .Right;

        mainScrollView.addSubview(title);

        let catName: UILabel = UILabel(frame: CGRectMake(200, 224, 120, 21));
        catName.text = selectedItem.catName
        catName.textAlignment = .Right;

        mainScrollView.addSubview(catName);

        let datePublished: UILabel = UILabel(frame: CGRectMake(0, 224, 200, 21));
        datePublished.text = selectedItem.dateCreated
        datePublished.textAlignment = .Left

        mainScrollView.addSubview(datePublished);

        let grayColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
        let iconsView: UIView = UIView(frame: CGRectMake(0, 250, self.view.frame.size.width, 32));
        iconsView.backgroundColor = grayColor
        mainScrollView.addSubview(iconsView);

        if (KhawaterDataManager.instance.banners.itemBanners.count > 0) {

            let awesomeView3: UIImageView = UIImageView(frame: CGRectMake(0, 282, self.view.frame.size.width, 50))
            awesomeView3.load(KhawaterDataManager.instance.banners.itemBanners[0].image);
            mainScrollView.addSubview(awesomeView3);
        }


        // test

        let catName1: UILabel = UILabel(frame: CGRectMake(0, 335, self.view.frame.size.width, 50));
        catName1.text = selectedItem.catName

        mainScrollView.addSubview(catName1);

        let catName2: UILabel = UILabel(frame: CGRectMake(0, 385, self.view.frame.size.width, 50));
        catName2.text = selectedItem.catName
        mainScrollView.addSubview(catName2);


        let catName3: UILabel = UILabel(frame: CGRectMake(0, 435, self.view.frame.size.width, 50));
        catName3.text = selectedItem.catName
        mainScrollView.addSubview(catName3);

        mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1000)
        mainScrollView.scrollEnabled = true;

        mainView.addSubview(mainScrollView);

请有人告诉我这里有什么问题吗?

2 个答案:

答案 0 :(得分:0)

请检查此行

mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 1000)

为什么内容高度为1000.这太多了。这应该是

mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 435+50)

另一个问题是为什么要将scollView设置为mainView的子视图而不是self.view

答案 1 :(得分:0)

此外,您可能想要添加所有子视图(awesomeView2,title,catName等) 到容器视图,然后添加容器视图以滚动视图。 类似的东西:

http://example.com/mortgage-brokers/broker_locations/victoria/melbourne