Ich habe das folgende Stück Code zu sehen:Konvertieren Visual Format Language in NSLayoutConstraint oder Layout-Anker?
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: .AlignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Vertical
scrollView.addSubview(stackView)
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[stackView]|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[stackView]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["stackView": stackView]))
for _ in 1 ..< 100 {
let vw = UIButton(type: UIButtonType.System)
vw.setTitle("Button", forState: .Normal)
stackView.addArrangedSubview(vw)
}
}
, die Teil eines einfachen demo ist, die eine UIStackView
Arbeit mit einem UIScrollView
macht.
Leider werden alle Einschränkungen mit Visual Format Language statt NSLayoutConstraint
oder der neuen IOS-Layout-Anker-API vorgenommen.
Ist es möglich, diese Datei mit den Einschränkungen der Visual Format Language, die durch NSLayoutConstraints
oder noch besser durch die Layout Anchor API ersetzt werden, neu zu schreiben und wie würde es aussehen?
Bisher habe ich dies versucht und es hat nicht funktioniert:
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true
scrollView.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
scrollView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
scrollView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Vertical
scrollView.addSubview(stackView)
stackView.heightAnchor.constraintEqualToAnchor(scrollView.heightAnchor).active = true
stackView.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true
stackView.topAnchor.constraintEqualToAnchor(scrollView.topAnchor).active = true
stackView.leadingAnchor.constraintEqualToAnchor(scrollView.leadingAnchor).active = true
for _ in 1 ..< 100 {
let vw = UIButton(type: UIButtonType.System)
vw.setTitle("Button2", forState: .Normal)
stackView.addArrangedSubview(vw)
}
}
Ja, ich habe ein Update gepostet, wo ich es selbst versucht habe, aber es hat nicht funktioniert. – Roymunson