UIImageView未在superview中按预期方式对齐

时间:2014-04-01 21:05:24

标签: ios objective-c uiscrollview uiimageview

我正在进行水平滚动的图像显示,并在自定义UIView中有一个UIScrollView。 Scrollview与自定义视图的高度相同,但不是很宽。我在设置图像时在for循环中创建了一个UIImageView。一切都运行正常,除了在加载时,UIImageView显示的Y轴比它应该更远,我无法弄清楚原因。这是代码......

- (void)createImageViewer {

NSLog(@"Entered createImageViewer");
// size attributes of the main view and scroll view
CGFloat mainViewHeight = self.mainView.frame.size.height; // height of the main view
CGFloat mainViewWidth = self.mainView.frame.size.width; // width of the the main view
CGFloat scrollViewHeight = self.scrollView.frame.size.height; // height of the scroll view
CGFloat scrollViewWidth = self.scrollView.frame.size.width; // width of the scroll view

self.arrayOfImages = [self loadImages];
self.scrollView.pagingEnabled = YES;

[self.mainView addSubview:self.scrollView];

for (int i = 0; i < [self.arrayOfImages count]; i++){
    UIImage *image = self.arrayOfImages[i];//Gets the image at the index i
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    //Shift each image over by the scrollView's width starting the first image at 0
    imageView.frame = CGRectMake(scrollViewWidth * i, 0, scrollViewWidth, scrollViewHeight);

    [self.scrollView addSubview:imageView];
}

int numberOfImages = [self.arrayOfImages count];

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numberOfImages, self.scrollView.frame.size.height);

}

我不允许发布图像,但基本上,图像垂直放置在我认为应该放置的大约50个点左右,即使是超视图也是如此。我期待这一点,因为scrollview是在故事板中创建的,并且与superview(相同的高度)均匀定位,但不是很宽。提前谢谢!

0 个答案:

没有答案