All uitableviewcells become one line in height on iOS 9

落花浮王杯 提交于 2019-12-05 13:48:56

I've run into a similar case. The trick seems to be to implement the dynamic sizing via UITableView's delegate methods explicitly. Even though settings automatic in storyboards is supposed to work - it doesn't. The solution is to provide UITableViewAutomaticDimension explicitly via the delegate method and then provide estimated cell sizes as you normally would:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    /* Return an estimated height or calculate 
     * estimated height dynamically on information 
     * that makes sense in your case.
     */
    return 200.0f;
}

If anyone knows precisely why this is necessary in iOS 9 and how it differs from iOS 8, I'd love to hear it.

We can use "UITableViewAutomaticDimension" in iOS 8 to implement the dynamic sizing explicitly by using following two properties:-

tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension

Then add following code in "cellForRowAtIndexPath" method before return cell.

cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()

And for iOS 9 we can add tableView's delegate method:-

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

There is a UIStackView issue on iOS 9-10 when using layoutMargins

extension UIStackView {
    open override func didMoveToSuperview() {   // a workaround for stackview issues on iOS 9-10
        super.didMoveToSuperview()
        if #available(iOS 11.0, *) { } else {
            layoutMargins = self.layoutMargins
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!