2016-03-27 56 views
0

我有以下的問題,我已經創建廠景,滾動視圖包含的代碼添加的UIScrollView不滾動上述視圖2</p> <p>然後添加滾動視圖到VIEW1迅速

,我的滾動視圖不滾動的問題的一些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); 

請誰能告訴我這裏有什麼問題嗎?

回答

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子視圖。

0

此外,您可能希望將所有子視圖(awesomeView2,title,catName等) 添加到容器視圖,然後添加容器視圖以滾動視圖。 類似於:

let containerView: UIView = { 
    let view = UIView() 
    view.addSubview(self.awesomeView2) 
    view.addSubview(self.title) 
    view.addSubview(self.catName) 
    view.addSubview(self.datePublished) 
    return view 
}() 

mainScrollView.addSubview(containerView)