以编程方式将约束添加到UIImageView

时间:2013-04-26 03:50:18

标签: ios uiscrollview uiimageview nslayoutconstraint

更新:这似乎与自动布局无关。即使我禁用自动布局,我仍然会得到相同的结果:(

我在滚动视图中添加了一系列UIImageView,并在花了数小时试图诊断之后 问题,我得出结论,它必须是一个约束问题,推动我的UIImage向下。请参阅附图:

enter image description here

正如您所看到的那样UIImage被按下,因为我已将UIScrollView背景标记为红色。我还附上了UIScrollView的约束图片:

enter image description here

以下是我的滚动视图的属性:

enter image description here

如何在UIImageView上设置约束,使其以UIScrollView水平居中?我使用以下代码用图像填充UIScrollView

for (UIView *v in self.scrollview.subviews)
    {
        [v removeFromSuperview];
    }

    CGRect workingFrame = self.scrollview.frame;
    workingFrame.origin.x = 0;

    for(NSDictionary *dict in info)
    {

        UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];

        MyManager * myManager = [MyManager sharedInstance];
        [myManager.assets addObject:imageview];
        [imageview setContentMode:UIViewContentModeScaleAspectFit];
        imageview.frame = workingFrame;
        [self.scrollview addSubview:imageview];
        workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
    }

    [self.scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];

1 个答案:

答案 0 :(得分:0)

workingFrame.origin.y = 0;修好了