我正在努力学习ScrollView的工作方式。我在scrollview上观看视频教程,你必须添加一个scrollview,然后是UIView,然后添加约束,并将View Controller设置为自由形式,并将高度设置为1000.
在我的应用中,我正在加载3张优惠券,这些优惠券是图片视图,这些图片视图是以编程方式设置的。这是代码,任何人都可以帮我滚动浏览器?感谢。
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)
}
}
}
答案 0 :(得分:0)
您将图像添加到viewController的视图中,而不是添加到scrollView中
您的viewController高度为1000,每张图像的垂直偏移量为50
它是这样的:
50 + 300 + 50 + 300 + 50 + 300 = 1050
它超过1000
答案 1 :(得分:0)
使用以下代码
let bottomOffset = CGPoint(x: 0, y: self.scrollView.contentSize.height - self.scrollView.bounds.size.height)
self.scrollView.setContentOffset(bottomOffset, animated: true)