简体   繁体   中英

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.

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;
}

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