2013-10-10 3 views
7

Lassen Sie uns sagen, ich habe eine Reihe von Ansichten, und ich möchte diese Ansichten in einer Liste stapeln. Nun, wenn ich im Voraus wissen, wie viele Ansichten sind, konnte ich eine Einschränkung wie folgt schreiben:Auto Layout visuelle Programmiersprache für eine Reihe von Ansichten

"V:|-[view0]-[view1]-[view2]-[view_n]" 

Doch wie kann ich so etwas wie dies mit einer variablen Anzahl von Ansichten in meinem Array erreichen?

Antwort

2

Sie können über das Array iterieren und die Zeichenfolge erstellen (verwenden Sie eine NSMutableString). Sie müssen die Ansichten einem Wörterbuch mit Schlüsseln hinzufügen, die mit den Namen übereinstimmen, die Sie in der Formatzeichenfolge verwenden.

2

Schauen Sie sich diese awesome Kategorie:

https://github.com/jrturton/UIView-Autolayout

Es hat eine Methode, die Sie spaceviews auf dem Behälter Ansicht und wird Raum eine Reihe von Ansichten gleichmäßig entlang der angegebenen Achse aufrufen können.

Es gibt Beispielcode im Demoprojekt, der alles abdecken sollte.

So würden Sie einige Ansichten gleichmäßig über die vertikale Achse verteilen:
Diese Mitte 4 Ansichten auf der X-Achse und beschränken Sie die Breite auf 150 Punkte. Die Höhe würde dann berechnet werden, abhängig von der Höhe der self.view

#import "UIView+AutoLayout.h" 

... 

- (void)spaceViews 
{ 
    NSArray *views = @[ [self spacedView], [self spacedView], [self spacedView], [self spacedView] ]; 

    [self.view spaceViews:views onAxis:UILayoutConstraintAxisVertical withSpacing:10 alignmentOptions:0]; 
} 

- (UIView *)spacedView 
{ 
    //Create an autolayout view 
    UIView *view = [UIView autoLayoutView]; 

    //Set the backgroundColor 
    [view setBackgroundColor:[UIColor redColor]]; 

    //Add the view to the superview 
    [self.view addSubview:view]; 

    //Constrain the width and center on the x axis 
    [view constrainToSize:CGSizeMake(150, 0)]; 
    [view centerInContainerOnAxis:NSLayoutAttributeCenterX]; 

    //Return the view 
    return view; 
} 
+0

Seien Sie sicher, meine Gabel des Projekts zu überprüfen, die neu entwickelt und reinigt die API und enthält viel mehr Funktionalität (einschließlich vieler Methoden zum Einschränken von Ansichtenanordnungen): https://github.com/smileyborg/UIView-AutoLayout – smileyborg

0

ich eine Anforderung hatte hinzuzufügen Ansichten von einem Array zu einem Scrollview für meine Tutorial Seiten, in diesem Fall hat ich durch Looping durch die Ansichten, die den VFL-String gebaut , unten ist ein Schnappschuss, dieser Code soll die Unteransicht vollständig in die Scrollview-Seite passen. Mit einigen Optimierungen kann Polsterung usw. hinzugefügt werden. Wie auch immer es hier posten, damit es jemandem hilft.

Voll Code arrayAutolayout

/*! 
Create an array of views that we need to load 
@param nil 
@result creates array of views and adds it to scrollview 
*/ 
-(void)setUpViews 
{ 
    [viewsDict setObject:contentScrollView forKey:@"parent"]; 
    int count = 20;//Lets layout 20 views 
    for (int i=0; i<=count; i++) { 
     // I am loading the view from xib. 
     ContentView *contenView = [[NSBundle mainBundle] loadNibNamed:@"ContentView" owner:self options:nil][0]; 
     contenView.translatesAutoresizingMaskIntoConstraints = NO; 
     // Layout the text and color 
     [contenView layoutTheLabel]; 
     [contentScrollView addSubview:contenView]; 
     [viewsArray addObject:contenView]; 
    } 
} 
/*! 
Method to layout the childviews in the scrollview. 
@param nil 
@result layout the child views 
*/ 
-(void)layoutViews 
{ 
    NSMutableString *horizontalString = [NSMutableString string]; 
    // Keep the start of the horizontal constraint 
    [horizontalString appendString:@"H:|"]; 
    for (int i=0; i<viewsArray.count; i++) { 
     // Here I am providing the index of the array as the view name key in the dictionary 
     [viewsDict setObject:viewsArray[i] forKey:[NSString stringWithFormat:@"v%d",i]]; 
     // Since we are having only one view vertically, then we need to add the constraint now itself. Since we need to have fullscreen, we are giving height equal to the superview. 
     NSString *verticalString = [NSString stringWithFormat:@"V:|[%@(==parent)]|", [NSString stringWithFormat:@"v%d",i]]; 
     // add the constraint 
     [contentScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalString options:0 metrics:nil views:viewsDict]]; 
     // Since we need to horizontally arrange, we construct a string, with all the views in array looped and here also we have fullwidth of superview. 
     [horizontalString appendString:[NSString stringWithFormat:@"[%@(==parent)]", [NSString stringWithFormat:@"v%d",i]]]; 
    } 
    // Close the string with the parent 
    [horizontalString appendString:@"|"]; 
    // apply the constraint 
    [contentScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontalString options:0 metrics:nil views:viewsDict]]; 
} 

Im Folgenden finden Sie die Zeichenfolge

erstellt
H:|[v0(==parent)][v1(==parent)][v2(==parent)][v3(==parent)][v4(==parent)][v5(==parent)][v6(==parent)][v7(==parent)][v8(==parent)][v9(==parent)][v10(==parent)][v11(==parent)][v12(==parent)][v13(==parent)][v14(==parent)][v15(==parent)][v16(==parent)][v17(==parent)][v18(==parent)][v19(==parent)][v20(==parent)]|