Cannot update UI object's frame in UITableViewCell

后端 未结 6 1972
眼角桃花
眼角桃花 2021-01-19 02:18

I have a subclassed UITableViewCell.

I need to dynamically change the frame of a UILabel.

Here\'s what I\'ve done:

- (UITableViewCell *)table         


        
6条回答
  •  一生所求
    2021-01-19 02:46

    Well a few days after posting the bounty I've found the answer, I hope this will help someone someday...

    I am not 100% sure why, but what I tried to do simply can not be done:

    You cannot change the frame of an object if you've put it through Interface Builder.

    To make the changes I wanted to do, I simply created those objects programmatically in my subclass, inside "initWithCoder".

    content = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 200, 50)];
    content.opaque = NO;
    content.numberOfLines = 0;
    [self.contentView addSubview:content];
    

    I was then able to change the frames inside "- (void)layoutSubviews" (but basically anywhere I wanted)

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        [cell.Content setFrame:CGRectMake(0, 0, 10, 10)];
    
        ...
    

提交回复
热议问题