UIScrollView的子视图中添加纯水平与自动布局循环 [英] UIScrollview add subview horizontally pure autolayout with for loop

查看:129
本文介绍了UIScrollView的子视图中添加纯水平与自动布局循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的的UIView 层次:

-UIView
 -UIScrollView

我对的UIScrollView 与有关它的约束是超级观点很简单:

My constraint for UIScrollview with relation to it's super view are very simple:

@"H:|-%f-[%@]-%f-|"

@"V:|-%f-[%@]-%f-|"

它们是可以预期的工作。

我想添加一个的UIImageView 为滚动型卧式子视图。
所以我的观点的层次结构将变为:

I am trying to add a UIImageView as subview of scrollview Horizontal. So my view hierarchy will become:

-UIView
 -UIScrollView
  -UIImageView

我加入的UIImageView 作为使用子视图以编程方式在的UIScrollView A 循环。

I am adding UIImageView as subview programmatically in UIScrollView using a for loop.

循环,我怎么能做到:

In the for loop, how can I achieve:

[SuperView]-10-[scrollview]-10-[UIImageView]-10-[UIImageView]-10-[UIScrollView]-10-[SuperView]

[SuperView]-10-[scrollview]-10-[UIImageView]-10-[UIImageView]-10-[UIScrollView]-10-[SuperView]

有问题的部分是加粗部分。
我曾尝试:

The problematic section is the bold part. What I have tried:

for(int i=1;i<3;i++)
        {
            UIImageView *image = [[UIImageView alloc] init];
            [image setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
            image.translatesAutoresizingMaskIntoConstraints = NO;
            [_scrollView addSubview:image];

            UIView *superView = _scrollView;



            NSDictionary * views = NSDictionaryOfVariableBindings(superView, image);

            NSString *formate = [NSString stringWithFormat:@"H:|-%f-[%@]-%f-|", scrollViewLeftMarginFromParent, @"image", scrollViewRightMarginFromParent];

            NSArray * WIDTH_CONSTRAINT = [NSLayoutConstraint constraintsWithVisualFormat:formate options:0 metrics:nil views:views];

            formate = [NSString stringWithFormat:@"V:|-%f-[%@]-%f-|", scrollViewTopMarginFromParent, @"image", scrollViewBottomMarginFromParent];

            NSArray * HEIGHT_CONSTRAINT = [NSLayoutConstraint constraintsWithVisualFormat:formate options:0 metrics:nil views:views];
            [superView addConstraints:WIDTH_CONSTRAINT];
            [superView addConstraints:HEIGHT_CONSTRAINT];
        }

我能想到的办法:

The approach I can think of:

    LeftSide:
    [scrollview]-10-[UIImageView]
    Right side:
    [UIImageView]-10-[scrollview]
    in between:
    [UIImageView]-10-[UIImageView]

如果这是正确的做法,那我怎么在达到这一目的循环。

If it's the right approach, then how do I achieve this in for loop.

如果它不是那么什么是最好的办法。

If it's not then what is best approach.

推荐答案

这是很简单的实际。你的做法是正确的,你需要的是你如何转换到这code。我会尽量简化为您服务。我假设一个UIImageView的宽度放大器;高度为100,只要你喜欢你可以改变

It's quite simple actually. Your approach is correct, all you need is how you convert that into code. I will try to simplify this for you. I am assuming a UIImageView's width & height as 100. You can change as you like

-(void)setUI
{
    lastView = nil;   //Declare a UIImageView* as instance var.
    arrayCount = [array count]; //In your case a static count of 3

    for(NSInteger index =0; index < arrayCount; index++)
    {
        UIImageView *view = [[UIImageView alloc] init];
        [self.mainScroll addSubview:view];

        [view setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(100)]-20-|" options:0 metrics:nil views:@{@"view":view}]];

        //--> If view is first then pin the leading edge to main ScrollView otherwise to the last View.
        if(lastView == nil && index == 0) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view(100)]" options:0 metrics:nil views:@{@"view":view}]];
        }
        else {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastView]-10-[view(100)]" options:0 metrics:nil views:@{@"lastView":lastView, @"view":view}]];
        }
        //--> If View is last then pin the trailing edge to mainScrollView trailing edge.
        if(index == arrayCount-1) {
            [self.mainScroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view]-10-|" options:0 metrics:nil views:@{@"view":view}]];
        }
        //--> Assign the current View as last view to keep the reference for next View.
        lastView = view;
    }
}

这篇关于UIScrollView的子视图中添加纯水平与自动布局循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆