尝试在UICollectionViewCell上设置子视图的框架

时间:2015-06-11 06:24:52

标签: ios iphone

我尝试使用下面的代码在UICollectionViewCell上设置子视图(图像和视图)的位置和大小

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        //wangzheng +
        self = [super initWithFrame:frame];
        if (self) {
            NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"IndexCityMicroCosmeticCell"owner:self options:nil];
            UIView *bw = [nibView objectAtIndex:0] ;

            [self.contentView addSubview:bw];
            [bw setFrame:self.contentView.bounds];

            //wangzheng +

            if (IS_IPHONE_6P || IS_IPHONE_6) {

                _scaleRate=1.5;
            }
            else{
                _scaleRate=1;
            }

            img1.frame=CGRectMake(0, 0, 120*_scaleRate, 151*_scaleRate);

            NSLog(@"%f %f %f %f  ",img1.frame.origin.x,img1.origin.y,img1.frame.size.width,img1.frame.size.height);
            img2.frame=CGRectMake(120*_scaleRate, 0, 200*_scaleRate, 85*_scaleRate);
            img3.frame=CGRectMake(120*_scaleRate, 84*_scaleRate, 200*_scaleRate, 67*_scaleRate);


            bottomView.frame=CGRectMake(0,150*_scaleRate, 320*_scaleRate, 24*_scaleRate);
            imgAnnounce.frame=CGRectMake(0, 0, 119*_scaleRate, 24*_scaleRate);
            anUIView.frame=CGRectMake(119*_scaleRate, 0, 201*_scaleRate, 24*_scaleRate);


        }
        return self;
    }
    return self;
}

enter image description here

我查了一下,发现价值合适,但他们没有立即重新安置。

enter image description here

图像适合UICollectionViewCell,但不遵循UICollectionViewCell来增加/调整UICollectionViewCell的大小

您的评论欢迎

2 个答案:

答案 0 :(得分:1)

你在哪里分配img1 ..等?什么时候重新加载后生效?您是否尝试将代码放在- (void)layoutSubviews

如果是的话......

尝试在-drawRect内移动代码以执行自定义绘图/ ui更新..并调用setNeedsDisplay以触发drawRect并重新绘制组件/ UI

答案 1 :(得分:0)

当单元格的大小(cell.contentView)发生变化时,您应该设置子视图的位置和大小,尝试使用layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];

    [bw setFrame:self.contentView.bounds];

    //wangzheng +

    if (IS_IPHONE_6P || IS_IPHONE_6) {

        _scaleRate=1.5;
    }
    else{
        _scaleRate=1;
    }

    img1.frame=CGRectMake(0, 0, 120*_scaleRate, 151*_scaleRate);

    NSLog(@"%f %f %f %f  ",img1.frame.origin.x,img1.origin.y,img1.frame.size.width,img1.frame.size.height);
    img2.frame=CGRectMake(120*_scaleRate, 0, 200*_scaleRate, 85*_scaleRate);
    img3.frame=CGRectMake(120*_scaleRate, 84*_scaleRate, 200*_scaleRate, 67*_scaleRate);

    bottomView.frame=CGRectMake(0,150*_scaleRate, 320*_scaleRate, 24*_scaleRate);
    imgAnnounce.frame=CGRectMake(0, 0, 119*_scaleRate, 24*_scaleRate);
    anUIView.frame=CGRectMake(119*_scaleRate, 0, 201*_scaleRate, 24*_scaleRate);
}

或在autoresizingMask中设置子视图(图片和视图)的initWithFrame,如下所示:

img1.frame=CGRectMake(0, 0, 120*_scaleRate, 151*_scaleRate);
img1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
…
img2.frame=CGRectMake(120*_scaleRate, 0, 200*_scaleRate, 85*_scaleRate);
img2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

img3.frame=CGRectMake(120*_scaleRate, 84*_scaleRate, 200*_scaleRate, 67*_scaleRate);
img3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | img2.autoresizingMask
...