arachide
arachide

Reputation: 8066

try to set the frame of subview on UICollectionViewCell

I try to use the code below to set the position and size of subview(image and view) on an 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

I checked and found that the value are right, but they do not relocate immediately.

enter image description here

images fit to the UICollectionViewCell, but does not follow UICollectionViewCell to increase/adjust the size of UICollectionViewCell

Your comment welcome

Upvotes: 0

Views: 71

Answers (2)

0yeoj
0yeoj

Reputation: 4550

Where are your allocating img1.. etc? when did this takes effect after reload? have you tried placing the code inside - (void)layoutSubviews ?

if yes...

try moving your code inside -drawRectto perform custom drawing/ui update.. and call setNeedsDisplay to trigger drawRect and redraw the components/UI

Upvotes: 1

Bannings
Bannings

Reputation: 10479

You should set the position and size of subviews when the size of the cell(cell.contentView) changes, try to use 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);
}

or set the autoresizingMask of the subviews (image and view) in the initWithFrame, like so:

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
...

Upvotes: 0

Related Questions