gpt4 book ai didi

swift - Scrollview 没有滚动到底部

转载 作者:行者123 更新时间:2023-11-30 12:05:32 34 4
gpt4 key购买 nike

我正在努力解决 ScrollView 的工作原理。我观看了有关 ScrollView 的视频教程,其中您必须添加 ScrollView ,然后添加 UIView,然后添加约束,并将 View Controller 设置为自由形式并将高度设置为 1000。

在我的应用程序中,我加载了 3 张优惠券,它们是 ImageView ,并且这些 ImageView 是以编程方式设置的。这是代码,有人可以帮助我使用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

最佳答案

您将图像添加到 viewController 的 View 中,而不是 ScrollView 中。
您的 viewController 高度为 1000,每个图像的垂直偏移量为 50。
事情是这样的:
50 + 300 + 50 + 300 + 50 + 300 = 1050
已经超过1000了

关于swift - Scrollview 没有滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719919/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com