ios批量創建UIImageView,微博九宮格圖片展示

用UICellectionView也可以搞定,根據需求來選擇吧:)
在一個view中根據imgArray中圖片數量,動態創建多個UIImageView展示圖片,一行展示三個圖片,當多於三個時,圖片多一行,整個view也要增加高度。

    //取屏幕寬高
    float width = [UIScreen mainScreen].bounds.size.width;
    chooseImageView = [[UIView alloc] init];
    NSArray *imgArray = [[NSArray alloc] initWithObjects:@"1_leirobin.jpg",@"1_leirobin.jpg",@"1_leirobin.jpg",@"1_leirobin.jpg",@"1_leirobin.jpg",nil];
    NSInteger picCount = [imgArray count];
    //定義每個cell圖片
    for (int i=0;i<picCount;i++){
        UIImageView *imageCell = [[UIImageView alloc] initWithFrame:CGRectMake(width/3*(i%3), width/3*(i/3)+20, width/3, width/3)];
        imageCell.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[imgArray objectAtIndex:i]]];
        //每當第4個圖片時,增加一行,增加整個view的高度
        if (i%3 == 0) {
            [chooseImageView setFrame:CGRectMake(0, 100, width, chooseImageView.frame.size.height+width/3+20)];
        }
        [chooseImageView addSubview:imageCell];
    }
    [self.view addSubview:chooseImageView];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章