简体   繁体   中英

Adding UIImageView to UIScrollView programmatically

I'm trying to create programmatically a UIScrollView containing a UIImageView. The image of the UIImageView should be scrolled vertically, my problem is that the UIScrollView is never added to my main View. Here's the code

@property (strong, nonatomic) UIImageView *advertiseView;
@property UIScrollView *sv;

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        _sv = [[UIScrollView alloc] init];

        _advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,568)];
        UIImage *image = [UIImage imageNamed:@"abc.png"];

        _advertiseView.image = image;
        _advertiseView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
        [_sv addSubview:_advertiseView];
        _sv.contentSize = CGSizeMake(320,1136); // size of abc.png
        [self.view addSubview:_sv];
    }

What am i missing??

You don't set the ScrollView size. Try this:

_sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
int width = 0;
int height = 0;
for (int i=0; i<[menupicarray count]; i++)
{
    NSString *menupicname=[NSString stringWithFormat:@"%@",[menupicarray objectAtIndex:i]];
    UIImageView *imageView = [[UIImageView alloc] init ];
    [imageView setImageWithURL:[NSURL URLWithString:menupicname]
              placeholderImage:[UIImage imageNamed:@"NOmage.png"]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    imageView.frame = CGRectMake( MenuView.frame.size.width*i,0, MenuView.frame.size.width, MenuView.frame.size.height-5);
    [MenuView addSubview:imageView];
    width = imageView.frame.size.width;
    height = imageView.frame.size.height;
    MenuView.contentSize = CGSizeMake(width*i+imageView.bounds.size.width, height);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM