2013-10-10 2 views
7

私はビューの配列を持っていて、これらのビューをリストに積み重ねたいとします。私はそこにあるどのように多くの意見を事前に知っていれば今、私はこのような制約を記述することができます。ビューの配列のための自動レイアウトビジュアルプログラミング言語

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

しかし、どのように私は私の配列内のビューの数が可変で、このような何かを達成することができますか?

答えて

2

配列全体を反復処理して文字列を作成できます(NSMutableStringを使用)。書式文字列で使用する名前と一致するキーを使用して、ビューを辞書に追加する必要があります。それはあなたが均等に指定した軸に沿ってコンテナビューと意志スペースにビューの配列を呼び出すことができspaceViews方法があり

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

2

は、この素晴らしいカテゴリをチェックしてください。

デモプロジェクトには、すべてをカバーするいくつかのサンプルコードがあります。

縦軸にいくつかのビューを均等に配置する方法は次のとおりです。
x軸上に4ビューを表示し、幅を150ポイントに制限します。高さは、私はこのケースでは、私は意見をループでVFL文字列を構築し、私のチュートリアルページのscrollviewに配列からビューを追加する必要があった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

そのプロジェクトの私のフォークをチェックアウトしてくださいを作成しています(ビューの配列を制限する多くのメソッドを含む):https://github.com/smileyborg/UIView-AutoLayout – smileyborg

0

の高さに応じて計算されます以下はスナップショットです。このコードは、サブビューをscrollviewのページに完全に収めるためのものです。いくつかの微調整で、パディングなどを追加することができます。とにかく誰かを助けるようにここにそれを掲示する。

全コードの下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]]; 
} 

文字列は、デザインを再し、APIをクリーンアップし、より多くの機能が含まれて

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)]| 
関連する問題