在UIScrollView中居中UImageView

时间:2017-06-21 12:59:18

标签: ios objective-c uiscrollview uiimageview

我有一个uiscrollview,我用IB拖动,我试图在滚动视图中放置7个图像,在它的中心,左右一些空间,但到目前为止,我没有成功。谁能告诉我哪里错了。 enter image description here

这是我正在使用的代码。

NSInteger numberOfViews = 7;
for (int i = 0; i < numberOfViews; i++) {

    CGFloat xOrigin = i * self.svIntro.frame.size.width;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.svIntro.frame.size.width, self.svIntro.frame.size.height)];

    if(i == 0)
        [imgView setImage:[UIImage imageNamed:@"introScreen1.png"]];
    else if(i == 1)
        [imgView setImage:[UIImage imageNamed:@"introScreen2.png"]];
    else if(i == 2)
        [imgView setImage:[UIImage imageNamed:@"introScreen3.png"]];
    else if(i == 3)
        [imgView setImage:[UIImage imageNamed:@"introScreen4.png"]];
    else if(i == 4)
        [imgView setImage:[UIImage imageNamed:@"introScreen5.png"]];
    else if(i == 5)
        [imgView setImage:[UIImage imageNamed:@"introScreen6.png"]];
    else if(i == 6)
        [imgView setImage:[UIImage imageNamed:@"introScreen7.png"]];

    imgView.contentMode = UIViewContentModeScaleToFill;

    [self.svIntro addSubview:imgView];
}

self.svIntro.contentSize = CGSizeMake(self.svIntro.frame.size.width * numberOfViews, self.svIntro.frame.size.height);
self.svIntro.pagingEnabled = YES;

0 个答案:

没有答案