Change height of UITableView programmatically

后端 未结 3 1676
有刺的猬
有刺的猬 2021-01-26 05:56

I have a UITableView and I\'d like to change its height programmatically depending on the number of cells contained in it.

Can you help me?

3条回答
  •  温柔的废话
    2021-01-26 06:24

    Did you tried sizeToFit or sizeThatFits:?

    CGRect tFrame = tableView.frame;
    tFrame.size.height = MIN([tableView sizeThatFits: CGSizeMake(tFrame.size.width,
                                                                 CGFLOAT_MAX)].height,
                             MaximumTableHieght);
    tableView.frame = tFrame;
    

    Or try this (I don't remember which was working for me).

    CGRect tFrame = tableView.frame;
    tFrame.size.height = MIN(tableView.contentSize.height,
                             MaximumTableHieght);
    tableView.frame = tFrame;
    

提交回复
热议问题