2017-10-12 204 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),它的工作原理。非常感谢! –