2013-10-10 19 views
7

假設我有一個視圖數組,並且我想將這些視圖堆疊在一個列表中。現在,如果我事先知道有多少的觀點有,我可以寫這樣的約束:用於視圖陣列的自動佈局可視化編程語言

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

但是,我怎麼能做到這樣的事情有意見,我數組變量是多少?

回答

2

您可以迭代數組並生成字符串(使用NSMutableString)。您需要使用與您在格式字符串中使用的名稱匹配的鍵將視圖添加到字典中。

2

退房這個真棒類別:

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

它有一個spaceViews方法,你可以在容器視圖和意志的空間沿指定軸均勻意見數組來調用。

演示項目中有一些示例代碼應該涵蓋所有內容。

下面介紹如何在縱軸上均勻地分佈一些視圖:
該中心在x軸上顯示4個視圖,並將寬度限制爲150個點。高度會再視的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

一定要看看我的這個項目,它重新設計的叉子串並清理API,包括了更多的功能(包括許多限制視圖數組的方法):https://github.com/smileyborg/UIView-AutoLayout – smileyborg

0

我有一個要求,通過意見循環從陣列的意見添加到滾動型對我的教程頁面,在這種情況下,我建立了VFL串的高度來計算,下面是一個快照,這段代碼是將subview完全放入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]]; 
} 

下面是創建

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)]|