一个视图控制器

时间:2016-03-05 18:37:31

标签: ios swift scrollview

我想在视图控制器中放置两个水平移动的滚动视图。我把它们放了,但它没有用。

这是我的代码:

class HomeSceneViewController : UIViewController,UIScrollViewDelegate {

@IBOutlet var HomeSceneScrollView: UIScrollView!

@IBOutlet var HomeScenePageControl: UIPageControl!

@IBOutlet var HomeSceneImageView1: UIImageView!

@IBOutlet var HomeSceneImageView2: UIImageView!

@IBOutlet var HomeSceneScrollView2: UIScrollView!

@IBOutlet var HomeSceneImageView3: UIImageView!

@IBOutlet var HomeSceneImageView4: UIImageView!

@IBOutlet var HomeScenePageControl2: UIPageControl!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

   self.HomeSceneScrollView.frame = CGRectMake(0, 0, self.view.frame.width, self.HomeSceneScrollView.frame.height)
    let scrollViewWidth:CGFloat = self.HomeSceneScrollView.frame.width
    let scrollViewHeight:CGFloat = self.HomeSceneScrollView.frame.height

    let imgOne = UIImageView(frame: CGRectMake(0,0,scrollViewWidth/2,scrollViewHeight))
    imgOne.image = UIImage(named: "BEST3")
    let imgTwo = UIImageView(frame: CGRectMake(scrollViewWidth/2,0,scrollViewWidth/2,scrollViewHeight))
    imgTwo.image = UIImage(named: "BEST4")
    let imgThree = UIImageView(frame: CGRectMake(scrollViewWidth,0,scrollViewWidth/2,scrollViewHeight))
    imgThree.image = UIImage(named: "BEST1")
    let imgFour = UIImageView(frame: CGRectMake(scrollViewWidth*3/2,0,scrollViewWidth/2,scrollViewHeight))
    imgFour.image = UIImage(named: "BEST2")

    self.HomeSceneScrollView.addSubview(imgOne)
    self.HomeSceneScrollView.addSubview(imgTwo)
    self.HomeSceneScrollView.addSubview(imgThree)
    self.HomeSceneScrollView.addSubview(imgFour)

    self.HomeSceneScrollView.contentSize = CGSizeMake(self.HomeSceneScrollView.frame.width * 2,self.HomeSceneScrollView.frame.height)
    self.HomeSceneScrollView.delegate = self
    self.HomeScenePageControl.currentPage = 0




    self.HomeSceneScrollView2.frame = CGRectMake(0, 392, self.view.frame.width, self.HomeSceneScrollView2.frame.height)
    let scrollViewWidth2:CGFloat = self.HomeSceneScrollView2.frame.width
    let scrollViewHeight2:CGFloat = self.HomeSceneScrollView2.frame.height

    let imgOne1 = UIImageView(frame: CGRectMake(0,392,scrollViewWidth2/2,scrollViewHeight2))
    imgOne1.image = UIImage(named: "RECOMMEND1")
    let imgTwo2 = UIImageView(frame: CGRectMake(scrollViewWidth2/2,392,scrollViewWidth2/2,scrollViewHeight2))
    imgTwo2.image = UIImage(named: "RECOMMEND2")
    let imgThree3 = UIImageView(frame: CGRectMake(scrollViewWidth2,392,scrollViewWidth2/2,scrollViewHeight2))
    imgThree3.image = UIImage(named: "RECOMMEND3")
    let imgFour4 = UIImageView(frame: CGRectMake(scrollViewWidth2*3/2,392,scrollViewWidth2/2,scrollViewHeight2))
    imgFour4.image = UIImage(named: "RECOMMEND4")

    self.HomeSceneScrollView2.addSubview(imgOne1)
    self.HomeSceneScrollView2.addSubview(imgTwo2)
    self.HomeSceneScrollView2.addSubview(imgThree3)
    self.HomeSceneScrollView2.addSubview(imgFour4)

    self.HomeSceneScrollView2.contentSize = CGSizeMake(self.HomeSceneScrollView2.frame.width * 2,self.HomeSceneScrollView2.frame.height)
    self.HomeSceneScrollView2.delegate = self
    self.HomeScenePageControl2.currentPage = 0

我编写了相同的代码,修改了一些像CGRectMake这样的细节。 第一个滚动视图效果很好。(UIImage名称" Best1,2,3,4",homescenescrollview)但是当我构建它时,第二个滚动视图不会出现。这是图像。sample image

问题是什么?请帮我!找不到任何解决方案..

1 个答案:

答案 0 :(得分:1)

因此,从代码的外观来看,似乎您在代码中不必要地设置了很多东西。我建议以编程方式在StoryBoard 中进行设置,而不是两者都设置。

此外,似乎您正在寻找像UICollectionView而不是UIScrollViews这样的东西。您尝试实现的目标可以通过两个单行,可水平滚动的CollectionView来完成。

开始研究UICollecitonViews的一些好地方是: