Reputation: 4363
I am using a UITableViewController in my app. This works great, but I was wondering how I can do some small changes per device. The UITableViewCell height is not on all devices great. On iPhone 6/7 and Plus models it fits perfect, but on an iPhone SE/5 and 4 the rows are too big. I know that Interface Builder has a function called Vary of Traits
, but when I select for example iPhone 4 and change the height of the UITableViewCell it will also be applied to all other sizes. So is this - different cell heights for different devices - possible and if yes, how can I add different cell heights based on different device heights?
Upvotes: 0
Views: 752
Reputation: 428
you can calculate height of cell at runtime in
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
}
for Example : let's assume that perfect cell height is 70 on Iphone 7 of view height 700,so you will code like
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return self.view.frame.size.height * 0.1
}
so using this method your cell will always size 10% of your view's height
Upvotes: 0
Reputation: 3233
UITableView
has a delegate method called heightForRowAt indexPath:
Within that method, you can write logic that defines the cell height as a proportion of screen height, tableView height, or whatever you need
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return // screenHeight (or tableViewHeight) / someNumberThatFitsYourNeeds
}
Upvotes: 1