Ich habe ein UIScrollView
die UIStackView
mit drei Objekte im Inneren hat (Label, Image und Label)Wie UIStackView innerhalb eines UIScrollView
hinzufügen Wie kann ich die Scroll Arbeit mit dem stackview lassen?
Wie Sie im Code sehen, fügte ich die Label11, image1 und label12 zum Stapel , aber ich möchte die Stapelelemente ändern beim Scrollen zu label21, image2 und label22 und ich weiß nicht wie!
Wie kann ich das tun? Hier ist, was ich habe versucht:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// parent scroll
let ScrollView = UIScrollView()
ScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 667)
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 667)
ScrollView.backgroundColor = UIColor.init(colorLiteralRed: 219, green: 237, blue: 254, alpha: 1)
ScrollView.pagingEnabled = true
//label1
let label11 = UILabel()
label11.text = "label1"
// image1
let image1 = UIImageView()
image1.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, 200)
image1.image = UIImage(named: "mother1")
//label2
let label12 = UILabel()
label12.text = "label2"
//label21
let label21 = UILabel()
label21.text = "label1"
// image2
let image = UIImageView()
image.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, 200)
image.image = UIImage(named: "mother1")
//label22
let label22 = UILabel()
label22.text = "label2"
//Stackview
let stackView = UIStackView(arrangedSubviews: [label11, image1, label12])
stackView.distribution = .FillEqually
stackView.axis = .Vertical
stackView.alignment = .Fill
stackView.spacing = 0
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.backgroundColor = UIColor.blueColor()
ScrollView.addSubview(stackView)
let margins = ScrollView.layoutMarginsGuide
stackView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor, constant: 0).active = true
stackView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 0).active = true
stackView.topAnchor.constraintEqualToAnchor(margins.topAnchor, constant: 0).active = true
stackView.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor, constant: 0).active = true
stackView.widthAnchor.constraintEqualToAnchor(ScrollView.widthAnchor, constant: 0).active = true
stackView.heightAnchor.constraintEqualToAnchor(ScrollView.heightAnchor, constant: 0).active = true
self.view.addSubview(ScrollView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sie müssen etwas mehr erklären, was Sie wollen und wie das anders ist als das, was Sie jetzt haben. "Die Stapelobjekte sollten sich ändern, wenn zu label21, image2 und label22 gescrollt wird" ist nicht klar. –
Sie haben Recht. Ich habe geklärt, indem ich den Teil hinzugefügt habe, von dem ich nicht weiß, wie. Danke! – user3126427
Ich hatte das gleiche Problem. Ich war frustriert, es dynamisch zu machen, so wie ich es auch bevorzuge. Hoffe jemand erklärt dieses uistackview Geschäft. – user1019042