我正在努力與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)
}
}
}
你嘗試填充券viewDidLoad中()?看起來好像在插入視圖後插入優惠券,包括滾動視圖。 –
你好,他們在視圖中出現了。我也調用了函數viewDidLoad(),但仍然沒有運氣。優惠券圖像正在出現,但它只是在第三張優惠券上,它被切斷並且不滾動到下一頁。我也設置了Paging爲scrollView啓用。 –
用戶交互已啓用,對不對? –