簡體   English   中英

以編程方式將UIImageView添加到UIScrollView

[英]Adding UIImageView to UIScrollView programmatically

我正在嘗試以編程方式創建一個包含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];
    }

我想念什么?

您沒有設置ScrollView的大小。 嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM