以编程方式使用自动布局添加垂直空间

时间:2013-08-06 08:20:51

标签: ios autolayout

我正在尝试以编程方式将自定义视图添加到现有的UIScrollView。我上网并找到了可能的解决方案,但解决方案搞乱了现有的Autolayout。我需要在自定义视图的超级视图(即滚动视图)和自定义视图之间添加一个垂直空间。因此,无论何时用户从较大的屏幕(iPhone5)切换到较小的屏幕(比如iPhone4s),反之亦然,在屏幕上相应地调整自定义视图。

-(void) createEventsOnScroll
{
    [imageScroller setContentSize:CGSizeMake(self.imageArray.count*imageScroller.frame.size.width, imageScroller.frame.size.height)];
    [imageScroller setContentOffset:CGPointMake(imageScroller.frame.size.width*self.selectedEventIndex,0)];
    imageScroller.showsHorizontalScrollIndicator = NO;

    for (int i = 0; i<self.imageArray.count; i++)
    {
        EventCustomView *event = [[EventCustomView alloc]initWithFrame:CGRectMake(i*320+5, 3, 310, 435)];
        ChildImage *child = [self.imageArray objectAtIndex:i];
        NSData *data = [NSData dataWithContentsOfFile:child.imageURL];
        event.eventImage.image = [UIImage imageWithData:data];
        [event.eventImage setContentMode:UIViewContentModeScaleAspectFit];
        event.noteView.delegate = self;

        [imageScroller addSubview:event];

        event.translatesAutoresizingMaskIntoConstraints = NO;
        imageScroller.translatesAutoresizingMaskIntoConstraints = NO;

        NSMutableArray *constraints = [[NSMutableArray alloc]init];

        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[event]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(event)]];

        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[event]-5-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(event)]];
    }

    imageScroller.bounces = NO;
    imageScroller.delegate = self;
    [imageScroller setClipsToBounds:NO];
    imageScroller.scrollEnabled = YES;

    imageScroller.pagingEnabled = YES;   
}

先谢谢。

0 个答案:

没有答案