2017-10-12 267 views
0

我正在努力與ScrollView的工作方式。我觀看了scrollview上的視頻教程,你必須添加一個滾動視圖,然後UIView,然後添加約束,並設置視圖控制器爲自由形式,並將高度設置爲1000.滾動查看不滾動到底部

在我的應用程序中,我正在加載3優惠券是圖像視圖,這些圖像視圖是以編程方式設置的。這裏的代碼,任何人都可以請幫我使用scrollViews?謝謝。

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var scrollView: UIScrollView! 

@IBOutlet weak var UIView: UIView! 

struct MyCoupons { 
    static var couponsString: UIImage? 
    static var siteCoupons: [String?] = [] 
    static var count: Int = 0 
} 

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

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func viewDidAppear(_ animated: Bool) { 
    var i: Int = 0 
    var x: Int = 0 
    var y: Int = 0 
    var width: Int = 0 
    var height: Int = 0 

    ViewController.PopulateDataCoupons() 

    i=0 
    height = 300 
    repeat { 
     let imageName = MyCoupons.siteCoupons[i]//"Coupons1.jpg" 
     let image = UIImage(named: imageName!) 
     let imageView = UIImageView(image: image!) 

     y = i * height + 50 
     imageView.frame = CGRect(x: 50, y: y, width: 250, height: 300) 

     view.addSubview(imageView) 
     i += 1 

    } 
     while (i < MyCoupons.count-1) 
} 

public class func PopulateDataCoupons() { 
    var i = 0 
    var abc: String 


    if let path = Bundle.main.path(forResource: "Coupons", ofType: "txt") { 
     do { 
      let data = try String(contentsOfFile: path, encoding: .utf8) 
      let myStrings = data.components(separatedBy: .newlines) 
      abc = myStrings.joined(separator: ",") 

      let couponsArr = abc.components(separatedBy: ",") 
      print("couponsArr.count", couponsArr.count) 

      repeat { 
       MyCoupons.siteCoupons.append(couponsArr[i]) 
       i+=1 
      } while i < ((couponsArr.count)-1) 

      MyCoupons.count = couponsArr.count 
     } catch { 
      print(error) 
     } 
    } 

} 

} Scrollers show but it won't go down after the bottom hits

Constraints

+0

你嘗試填充券viewDidLoad中()?看起來好像在插入視圖後插入優惠券,包括滾動視圖。 –

+0

你好,他們在視圖中出現了。我也調用了函數viewDidLoad(),但仍然沒有運氣。優惠券圖像正在出現,但它只是在第三張優惠券上,它被切斷並且不滾動到下一頁。我也設置了Paging爲scrollView啓用。 –

+0

用戶交互已啓用,對不對? –

回答

0

您要添加的圖片到你的viewController的視圖,而不是你的滾動視圖。
您的viewController高度爲1000,每個圖像的垂直偏移量爲50。
它是這樣的:
50 + 300 + 50 + 300 + 50 + 300 = 1050
這是1000

+0

那麼如何以編程方式將我的圖像添加到scrollView?謝謝。順便說一句,我把高度改爲1200而不是1000,但仍然有問題。 –

+0

scrollView.addsubviews(_ :)應該做的伎倆。 – GBaeOne

+0

酷!我改變了行scrollView.addSubview(imageView),它的工作原理。非常感謝! –