Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep an aspect ratio of UITableViewCell using Auto layout?

I'd like to keep aspect ratio of UITableViewCell using Auto layout. I want to set the width of a tableViewCell as

UIScreen.mainScreen().bounds.size.width - 10

Then, I'd like to set the height of the cell as 1.2*width. If you know how to do it, or some references, please tell me. Thank you for your kindness.

like image 777
Toshi Avatar asked Jan 29 '26 22:01

Toshi


1 Answers

You can use UITableViewDelegate protocol function heightForRowAtIndexPath:

Swift:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.frame.width * 1.2
}

Objective C:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return tableView.frame.size.width * 1.2;
}
like image 134
Vitalii Gozhenko Avatar answered Jan 31 '26 12:01

Vitalii Gozhenko