Wenn ich mehrere Unteransichten zu meinem UIScrollView hinzufügen, ändert sich die contentsSize
der ScrollView nicht automatisch (wie die Richtlinien vorschlagen, sollten sie).UIScrollView Inhalt Größe Problem mit Auto Layout
Wenn ich die contentsSize
auf 6 mal die Breite einstellen (ich mache eine 6-seitige Scroll-Ansicht), sind die Ansichten richtig ausgelegt. Aber ich manuell nicht eingestellt contentsSize
, ScrollView wird nicht scrollen und contentsSize
ist (0,0)
.
Die offensichtliche Antwort ist, nur die contentSize
manuell einzustellen, aber ich bin nervös, dass ich ein grundsätzlicheres Problem habe (oder Missverständnis mit Auto-Layout), das mich später verletzen wird. Mein Code ist unten:
override func viewDidLoad() {
super.viewDidLoad()
//Adding a Paging ScrollView to ViewController
let sv = UIScrollView()
view.addSubview(sv)
sv.pagingEnabled = true
let allC1 = NSLayoutConstraint.constraintsWithVisualFormat("H:|[sv]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["sv": sv])
let allC2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[sv]|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["sv": sv])
NSLayoutConstraint.activateConstraints(allC1+allC2)
sv.translatesAutoresizingMaskIntoConstraints = false
var subViewConstraints:[NSLayoutConstraint] = []
var mySubViews:[UIView] = []
//First SubView Add & Layout
let firstView = UIView(withIndex: 0)
mySubViews.append(firstView)
sv.addSubview(firstView)
let cWidth = NSLayoutConstraint(item: tempView, attribute: .Width, relatedBy: .Equal, toItem: sv, attribute: .Width, multiplier: 1.0, constant: 0.0)
let cHeight = NSLayoutConstraint(item: tempView, attribute: .Height, relatedBy: .Equal, toItem: sv, attribute: .Height, multiplier: 1.0, constant: 0.0)
var subViewConstraints :[NSLayoutConstraint] = [cWidth, cHeight]
//Second through Sixth SubView Add & Layout
for i in 1..<6 {
let tempView = UIView()
mySubViews.append(tempView)
sv.addSubview(tempView)
let cWidth = NSLayoutConstraint(item: tempView, attribute: .Width, relatedBy: .Equal, toItem: mySubViews[i-1], attribute: .Width, multiplier: 1.0, constant: 0.0)
let cHeight = NSLayoutConstraint(item: tempView, attribute: .Height, relatedBy: .Equal, toItem: mySubViews[i-1], attribute: .Height, multiplier: 1.0, constant: 0.0)
let cLeading = NSLayoutConstraint(item: tempView, attribute: .Leading, relatedBy: .Equal, toItem: mySubViews[i-1], attribute: .Trailing, multiplier: 1.0, constant: 0.0)
subViewConstraints += [cWidth, cHeight, cLeading]
}
NSLayoutConstraint.activateConstraints(subViewConstraints)
}
Ich mache die grundlegende Sache, die ich Scroll View und dann eine Ansicht innerhalb der Bildlaufansicht hinzufügen, die alle Unteransichten enthalten wird. Definieren Sie die Höhenbeschränkungen für diese Ansicht, und das Problem ist gelöst. Was ich mache ist gleiche Breiten zu Scrollview, Top, Bottom, MaginLeft & Trailing Raum mit ScrollView & definiere explizite Höhe der View & Problem ist gelöst, ich mache keine Codierung alles wird von Storyboard behandelt –