2016-04-18 3 views
0

Ich habe meinen Code so abgespeckt, dass er einfach zu verstehen ist.UIScrollerView kann nicht mit reinen Auto-Layout-Einschränkungen erstellt werden

Angenommen, Sie haben einen Controller und möchten einen einfachen Scroller mit reinem Auto-Layout hinzufügen.

Sie können meine Funktionswerkzeug (zur Verfügung gestellt unten) wie folgt aufrufen:

// Create scroll view 
let strip = addStripCategoryTo(view) 

// Attach it to the view, vertically and horizontally 
strip.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true 
strip.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true 


// The function 
func addStripCategoryTo(parent: UIView) -> UIView { 

    let h:CGFloat = 128 
    let w = 2*h/3 
    let n = 5 
    let width = w * CGFloat(n) 
    let height = h 

    // Scroll view 
    let scrollview = UIScrollView() 
    parent.addSubview(scrollview) 

    scrollview.scrollEnabled = true 
    scrollview.translatesAutoresizingMaskIntoConstraints = false 
    scrollview.widthAnchor .constraintEqualToAnchor(parent.widthAnchor).active = true 
    scrollview.heightAnchor.constraintEqualToConstant(h).active = true 

    scrollview.layer.borderWidth = 2 
    scrollview.layer.borderColor = UIColor.greenColor().CGColor 
    scrollview.backgroundColor = UIColor.brownColor() 

    // Scroll view content 
    let contentView = UIView() //frame:CGRect(origin: CGPointZero, size:CGSize(width: width, height: height))) 
    scrollview.addSubview(contentView) 
    contentView.backgroundColor = UIColor.redColor() 
    contentView.layer.borderWidth = 10 
    contentView.layer.borderColor = UIColor.blueColor().CGColor 
    contentView.translatesAutoresizingMaskIntoConstraints = false 
    contentView.centerYAnchor.constraintEqualToAnchor(scrollview.centerYAnchor).active = true 
    contentView.widthAnchor .constraintEqualToConstant(width).active = true 
    contentView.heightAnchor.constraintEqualToConstant(height).active = true 

    return scrollview 
} 

Leider kann ich es nicht horizontal scrollen, siehe Screenshot wie folgt:

enter image description here

Was bin ich fehlt ?

Antwort

0

Hier ist die Lösung meiner Frage. Ich bot eine Werkzeugfunktion, die das Problem wie folgt lösen:

func setScrollViewConstraints(scrollView: UIScrollView, contentView:UIView, multiplier m:(width:CGFloat, height:CGFloat)=(1, 1)) { 

    guard let scrollViewParent = scrollView.superview else { 
     assert(false, "setScrollViewConstraints() requires the scrollview to have a superview") 
     return 
    } 

    guard let contentViewParent = contentView.superview else { 
     assert(false, "setScrollViewConstraints() requires the contentView to have a superview") 
     return 
    } 

    guard contentViewParent == scrollView else { 
     assert(false, "setScrollViewConstraints() requires the contentView to have a superview being the scrollView") 
     return 
    } 

    scrollView.translatesAutoresizingMaskIntoConstraints = false 
    scrollView.widthAnchor .constraintEqualToAnchor(scrollViewParent.widthAnchor, multiplier: m.width).active = true 
    scrollView.heightAnchor .constraintEqualToAnchor(scrollViewParent.heightAnchor, multiplier: m.height).active = true 


    contentView.translatesAutoresizingMaskIntoConstraints = false 
    contentView.leftAnchor .constraintEqualToAnchor(contentViewParent.leftAnchor  ).active = true 
    contentView.rightAnchor .constraintEqualToAnchor(contentViewParent.rightAnchor ).active = true 
    contentView.topAnchor .constraintEqualToAnchor(contentViewParent.topAnchor  ).active = true 
    contentView.bottomAnchor.constraintEqualToAnchor(contentViewParent.bottomAnchor ).active = true 

    /* 

// Pre-iOS 9 version 

let views = [ 
"scrollView":scrollview, 
"contentView":contentView 
] 

parent.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options:[], metrics: [:], views:views)) 
parent.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options:[], metrics: [:], views:views)) 

scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options:[], metrics: [:], views:views)) 
scrollview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[contentView]|", options:[], metrics: [:], views:views)) 
*/ 

} 

es so mithilfe des Scroll-Ansicht Breite von 100% der Superblick und 1/4 seiner Höhe für mit:

func addStripCategoryTo(parent: UIView) -> UIView { 

    // Scroll view 
    let scrollview = UIScrollView() 
    parent.addSubview(scrollview) 

    scrollview.backgroundColor = UIColor.brownColor() 
    scrollview.layer.borderWidth = 2 
    scrollview.layer.borderColor = UIColor.greenColor().CGColor 

    // Scroll view content 
    let contentView = UIImageView() 
    contentView.image = UIImage(named: "cover-0.jpeg") 
    scrollview.addSubview(contentView) 

    contentView.backgroundColor = UIColor.redColor() 
    contentView.layer.borderWidth = 10 
    contentView.layer.borderColor = UIColor.blueColor().CGColor 

    // Apply constraints 
    setScrollViewConstraints(scrollview, contentView:contentView, multiplier: (1, 0.25)) 

    return scrollview 
} 
0

Ihre Einschränkungen sollten so sein,

Scroll - oben, unten, führt,

InhaltAlle nachlauf - oben, unten, führt, hintere, feste Breite, vertikal Zentrum in Behälter (Mitte y)

und Ihre Breite Inhalt Ansicht sollte dann als Bildschirmbreite groß sein, können Sie auch horizontal Hoffnung bewegen diese diese eine Chance geben :)

+0

Es spielt keine (leider habe ich eine fehlende Zeile aus meinem Code und einem Screenshot hinzugefügt). –

+0

versuchen, Benutzerinteraktion zu aktivieren diese Zeile hinzuzufügen, 'scrollview.userInteractionEnabled = true 'Ich bin mir nicht sicher, aber versuchen, wenn es lösen kann – Lion

+0

Das gleiche, kein Scrollen. –

0

helfen wird, zu gehen und diese zu beantworten, ob es helfen s.

EDIT Answering Frage besser, gelöscht alte Antwort:

diese beiden Zeilen Hinzufügen sollte horizontal scrollen machen: contentView.leadingAnchor.constraintEqualToAnchor(scrollview.leadingAnchor).active = true contentView.trailingAnchor.constraintEqualToAnchor(scrollview.trailingAnchor).active = true

Das Problem ist, dass ohne diese beiden Einschränkung, die Scrollview nicht kenne die contentSize, also nicht scrollen, obwohl du dem contentView eine bestimmte Breite gegeben hast. Sie müssen die scrollView wissen lassen, wo diese contentView beginnt und endet, und diese beiden Einschränkungen helfen dem scrollView, zu wissen, wie groß die contentsView-Breite ist. Wenn nicht, wird die contentSize des scrollView gleich bleiben wie die Breite des Geräts, und der contentView wird nur außerhalb des Bildschirms angezeigt, da seine Breite größer ist als der Bildschirm des Geräts.

Hoffe das half besser.

Voll Code der Funktion

func addStripCategoryTo(parent: UIView)-> UIView { 

    let h:CGFloat = 128 
    let w = 2*h/3 
    let n = 5 
    let width = w * CGFloat(n) 
    let height = h 

    // Scroll view 
    let scrollview = UIScrollView() 
    parent.addSubview(scrollview) 

    scrollview.scrollEnabled = true 
    scrollview.translatesAutoresizingMaskIntoConstraints = false 
    scrollview.leadingAnchor.constraintEqualToAnchor(parent.leadingAnchor).active = true 
    scrollview.trailingAnchor.constraintEqualToAnchor(parent.trailingAnchor).active = true 
    scrollview.widthAnchor .constraintEqualToAnchor(parent.widthAnchor).active = true 
    scrollview.heightAnchor.constraintEqualToConstant(h).active = true 

    scrollview.layer.borderWidth = 2 
    scrollview.layer.borderColor = UIColor.greenColor().CGColor 
    scrollview.backgroundColor = UIColor.brownColor() 

    // Scroll view content 
    let contentView = UIView() //frame:CGRect(origin: CGPointZero, size:CGSize(width: width, height: height))) 
    scrollview.addSubview(contentView) 
    contentView.backgroundColor = UIColor.redColor() 
    contentView.layer.borderWidth = 10 
    contentView.layer.borderColor = UIColor.blueColor().CGColor 
    contentView.translatesAutoresizingMaskIntoConstraints = false 
    contentView.centerYAnchor.constraintEqualToAnchor(scrollview.centerYAnchor).active = true 
    contentView.leadingAnchor.constraintEqualToAnchor(scrollview.leadingAnchor).active = true 
    contentView.trailingAnchor.constraintEqualToAnchor(scrollview.trailingAnchor).active = true 
    contentView.widthAnchor.constraintEqualToConstant(width).active = true 
    contentView.heightAnchor.constraintEqualToConstant(height).active = true 

    return scrollview 
} 
+0

Dies beantwortet die Frage nicht. –

+0

Wie scrollt man horizontal, und ich gab ein paar Tipps, wie man scrollView mit Auto Layout erstellt und horizontal scrollt. Was habe ich verpasst? – iOSnewb

+0

Vergiss es, ich sehe jetzt. – iOSnewb

Verwandte Themen