以编程方式将UIImageView添加到UIScrollView

时间:2014-06-15 13:27:46

标签: ios iphone objective-c uiscrollview uiimageview

我尝试以编程方式创建包含UIImageView的UIScrollView。 UIImageView的图像应该垂直滚动,我的问题是UIScrollView永远不会添加到我的主视图中。 这是代码

@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];
    }

我错过了什么?

2 个答案:

答案 0 :(得分:1)

您没有设置ScrollView大小。试试这个:

_sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];

答案 1 :(得分:0)

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);
}