Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 向UIScrollView添加5个图像_Ios_Objective C_Uiscrollview - Fatal编程技术网

Ios 向UIScrollView添加5个图像

Ios 向UIScrollView添加5个图像,ios,objective-c,uiscrollview,Ios,Objective C,Uiscrollview,我有5个图像,我希望放在UIScrollView中,以便用户可以在它们之间滚动 如何做到这一点?最好使用UICollectionView,并将每个图像放在自己的单元格中。然后,所有的内容大小、定位和滚动都会为您处理。我建议使用UICollectionView,但如果您坚持使用UIScrollView,您可以执行以下操作: .h文件 @interface MyViewController : UIViewController @property (nonatomic, strong) UIScr

我有5个图像,我希望放在UIScrollView中,以便用户可以在它们之间滚动

如何做到这一点?

最好使用UICollectionView,并将每个图像放在自己的单元格中。然后,所有的内容大小、定位和滚动都会为您处理。

我建议使用UICollectionView,但如果您坚持使用UIScrollView,您可以执行以下操作:

.h文件

@interface MyViewController : UIViewController

@property (nonatomic, strong) UIScrollView *scv;

@end
m

这是一个非常简单的版本,有更复杂的版本,可以更容易地实现添加新图像,删除图像等


如果您还有任何问题想知道如何实现新功能,请提问。

例如,您可以使用谷歌UIScrollView教程。
@interface MyViewController()

@property (nonatomic, strong) NSMutableArray *imageArray;

@end

@implementation MyViewController

@synthesize scv = _scv, imageArray = _imageArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    _arrayOfImages = [[NSMutableArray alloc] init];
    [_arrayOfImages addObject:[UIImage imageNamed:@"yourImageName.png"]]; // Do this for as many UIImages you want to add.
    [self configureView];
}

- (void)configureView
{
    _scv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    float yposition = 0;
    for(int i = 0; i < [_arrayOfImages count]; i++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, yposition, self.view.frame.size.width-20, 100)];
        [img setImage:[_arrayOfImages objectAtIndex:i];
        [img setTag:i];
        [_scv addSubview:img];
        yposition =+ 100 + 10; // get the current yposition = yposition + img height + an extra 10 to add a gap.
    }    
    [_scv setContentSize:CGSizeMake(self.view.frame.size.height, yposition)]; // Set the width to match the screen size, and set the height as the final yposition.
    [[self view] addSubview:_scv];
}

@end