2016-06-02 9 views
1

Ich versuche, 2 UIScrollView in der Ansicht hinzuzufügen. Ich habe einen Scrollview hinzugefügt und es hat perfekt funktioniert. Dann habe ich andere scrollview hinzugefügt. Aber jetzt ist es in ersten Scroll Absturz ..Scrollview mit automatischem Layout programmgesteuert

func setupTopModelScrollView() 
{ 

    var viewBindingsDictBoth = [String: AnyObject]() 
    viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView 
    viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels 
    viewBindingsDictBoth["contentView"] = contentView 
    viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed 
    viewBindingsDictBoth["lblTitle"] = lblTitle 
    viewBindingsDictBoth["mainView"] = self.view 

    scrollViewTopModels = UIScrollView(frame:CGRectZero) 

    scrollViewTopModels.sizeToFit() 

    view.addSubview(scrollViewTopModels) 

    scrollViewTopModels.backgroundColor = UIColor.blueColor() 

    contentView = UIView() 
    contentView.backgroundColor = UIColor.redColor() 
    scrollViewTopModels.addSubview(contentView) 

    view.translatesAutoresizingMaskIntoConstraints = false 
    scrollViewTopModels.translatesAutoresizingMaskIntoConstraints = false 
    contentView.translatesAutoresizingMaskIntoConstraints = false 

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollViewTopModels]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth)) 

    let contentViewWidth : Int = arrTopModels.count * Int(SCREENWIDTH) 

    scrollViewTopModels.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[contentView]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth)) 
    scrollViewTopModels.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[contentView]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth)) 

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView(\(contentViewWidth))]|",options: [], metrics: nil, views:viewBindingsDictBoth)) 
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[contentView(==scrollViewTopModels)]",options: [], metrics: nil, views:viewBindingsDictBoth)) 

    scrollViewTopModels.showsHorizontalScrollIndicator = false 
    scrollViewTopModels.bounces = false 
    scrollViewTopModels.pagingEnabled = true 


    for i in 0..<arrTopModels.count 
    { 

     let topModelView = TopModelCell(frame:CGRectZero) 
     contentView.addSubview(topModelView) 

     let spaceFromLeft : Int = i * Int(SCREENWIDTH) 

     topModelView.translatesAutoresizingMaskIntoConstraints = false 
     topModelView.imgModel.image = UIImage(named: arrTopModels.objectAtIndex(i) as! String) 

     contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(spaceFromLeft)-[topModelView(\(SCREENWIDTH))]",options: [], metrics: nil, views:viewBindingsDictBoth)) 

     contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[topModelView(==contentView)]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth)) 

     print(topModelView) 

     contentView.contentMode = UIViewContentMode.Redraw 

    } 

} 

ich diese Störung erhalten und abstürzt ..

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
H:|-0-[scrollViewTopModels]-0-| 
          ^' 

Ich bin stecken .. Wenn jemand helfen kann.

Vielen Dank im Voraus ..

+1

Der Fehler klingt wie Ihre „Ansicht“ Variable nicht auf einen Superview hinzugefügt. Eine Sicht muss über ein Elternteil verfügen, bevor Einschränkungen hinzugefügt werden. –

+0

Nach dem folgenden @ john_ryan Kommentar sollten Sie folgendes beachten:> Ihre "view" Variable ist nicht in viewBindingsDictBoth. Sie müssen also viewBindingsDictBoth ["view"] = view auch haben. – Alok

Antwort

0

Ich habe die Antwort gefunden. Wir müssen das Wörterbuch nach dem Hinzufügen der Unteransicht deklarieren. sonst wird es nill.

var viewBindingsDictBoth = [String: AnyObject]() 
viewBindingsDictBoth["shortListedScrollView"] = shortListedScrollView 
viewBindingsDictBoth["scrollViewTopModels"] = scrollViewTopModels 
viewBindingsDictBoth["contentView"] = contentView 
viewBindingsDictBoth["contentViewShortListed"] = contentViewShortListed 
viewBindingsDictBoth["lblTitle"] = lblTitle 
viewBindingsDictBoth["mainView"] = self.view 

Wir haben den Code zu schreiben, nachdem die alle Ansichten Zuteilung ..