【发布时间】:2018-12-25 12:11:18
【问题描述】:
我正在尝试实现一个 UIScrollView 并使用 Objective-C 从 Xcode 中的图像数组中加载图像,UIScrollView 中的每个图像在纵向和横向模式下都必须是全屏的。我有能够使其在纵向模式下工作,但不能在横向模式下工作。它应该在所有 iOS 设备尺寸下都是全屏的。以下是我到目前为止编写的代码。我的故事板中有 UIScrollView、一个按钮和一个标签。任何答案或指向实现这一点的教程将不胜感激。提前致谢。
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat widthInPixel = screen.size.width;
CGFloat heightInPixel = screen.size.height;
float increaseAmount = widthInPixel;
self.imageScrollView.contentMode = UIViewContentModeScaleAspectFit;
self.imageScrollView.pagingEnabled = YES;
[self.imageScrollView setAlwaysBounceVertical:NO];
[self.imageScrollView setAlwaysBounceHorizontal:NO];
imageViews = [[NSMutableArray alloc] init];
self.imageScrollView.clipsToBounds = YES;
NSInteger imageNumbers = [self.images count];
UIImageView *image;
for(NSInteger i = 0; i < imageNumbers; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
widthInPixel,
self.imageScrollView.frame.size.height)];
image.contentMode = UIViewContentModeScaleAspectFit;
image.clipsToBounds = YES;
image.image = self.images[i];
[image setAutoresizingMask:
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
[self.imageScrollView addSubview:image];
}
self.imageScrollView.contentSize = CGSizeMake(image.frame.size.width *
imageNumbers,
self.imageScrollView.frame.size.height);
【问题讨论】:
-
为什么不使用滚动视图而不是使用 UICollectionView,这会减少很多代码。
-
@NisarAhmad,我需要用 UIScrollView 来实现它,如果您有任何其他建议,请告诉我,谢谢,不胜感激
-
相对于滚动视图以编程方式向 imageView 添加前导、尾随、顶部和底部约束
-
happyteamlabs.com/blog/…你可以相应地点击这个链接
-
@ucheGodfrey - 查看我编辑的答案...
标签: ios objective-c uiscrollview