自动布局可视化编程语言,用于一系列视图

假设我有一系列视图,并且我想将这些视图堆叠在一个列表中。 现在,如果我事先知道有多少个视图,那么我可以写一个像这样的约束:

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

但是,如何在数组中使用可变数量的视图来完成这样的事情?


你可以遍历数组并构建字符串(使用NSMutableString )。 您需要使用与您在格式字符串中使用的名称匹配的键将视图添加到字典中。


看看这个真棒类别:

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;
}

我需要从数组中添加视图到我的教程页面的滚动视图,在这种情况下,我通过循环遍历视图来构建VFL字符串,下面是一个快照,此代码用于将子视图完全放入滚动视图的页面。 随着一些调整,填充等可以添加。 无论如何张贴在这里,以便它可以帮助某人。

完整的代码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)]|
链接地址: http://www.djcxy.com/p/74885.html

上一篇: Auto layout visual programming language for an array of views

下一篇: Executing synchronous queries to Google Cloud Endpoints on iOS