简体   繁体   中英

Custom UIView(.xib) changes frame automatically after adding as subview in UITableViewCell

Due to requirement, I have made a custom UIView with .xib. The problem is when i alloc,init the uiview , the frame is all right but when i add the view as subview to UITableViewCell.contentView, the frame of my custom view dramatically changes. I even tried to enforce the frame after adding as subview but of no help.

I am using storyBoard with autolayout. the problem persists in both case when i set constrains in my custom view and when remove all constrains .

Thanks in advance.

So your issue is that you are not changing height of row when your adding view into your cell's content view. Here is below height method and changes that you need to implement:-

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if((indexPath.row==3 && rowSelected))  //You need to put condition check in which you need to see if that particular row is selected or not.
  //Then a flag for checking if view is added or not to it's contentView.
  //If both condition true then you need to provide a new height to that particular cell.

    return 220;  //120 height of your cell + 100 height of your view.
  else
    return 120;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM