Set different table view cell size on different iOS devices - ios

In app I set tableView cell height to fix 100 pts. It looks normal in iPhone 8 Plus. But when I run app for example, in iPhone 5s or iPhone 6 table cell looks very big in interface. How can I set cell height for different iOS devices?

You can set height relative to screen height or tableView height.
for example:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return tableView.frame.size.height/6
}

A quick solution could be
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100 * self.tableView.frame.size.height / HEIGHT_OF_TABLE_IN_IPHONE_8_PLUS
}
Here take a note of the self.tableView.frame.size.height, replace HEIGHT_OF_TABLE_IN_IPHONE_8_PLUS by that height. It will obviously give you the proportionate height according to device height.

Note that I would assume that it is the expected behavior for displaying the cells in different devices. You could notice that if you open contacts -for instance- on both iPhone 8 plus and iPhone 5s, you would notice that the iPhone 8 Plus displays more contacts than the smaller screen iPhone 5s.
However, if you are aiming to make the height of the cell to be proportional based on the screen height, you could achieve it by implementing tableView(_:heightForRowAt:) delegate method, like this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let screenHeight = UIScreen.main.bounds.height
return screenHeight / 10
}
means that the height of the cell would be 10% of the screen height, no matter what is the device.

Related

iOS 14 dequeueReusableCell issues

I have a chat view where I'm using tableview to display different custom cells for incoming and outgoing messages and the app was working fine till ios 13,
But as soon as I updated the phone OS to ios 14 its giving me issue
the issue is seems to be likely in dequeueReusableCell function, bigger cells taking height if smaller cells and smaller taking height of bigger cell instead of their own cell content
I have used the table height like this
IN VIEW DIDLOAD FUNCTION
self.chatTableView.estimatedRowHeight = 44
self.chatTableView.rowHeight = UITableView.automaticDimension
internal func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Also tried to use
cell.setNeedsLayout()
cell.layoutIfNeeded()
at differenct places like when the heightForRowAt, cellForRowAtIndexPAth and willdisplayCellForRowAtIndexPAth get called but getting the same issue

Changing UITableView Cell's Height by using storyboard

I'm using Swift5 and Xcode 11.4.1 on macOS Catalina.
main.storyboard
As you can see here, I selected tableview cell, went to the size inspector and changed the height as 100 and hit enter.
It is successfully changed as 100 height on storyboard but when I run this thing,
Simulator screenshot
It looks like this, the image link above.
The height is set as 44 not 100.
Add this method to the class that conform's to your UITableView's delegate to change the tableView's height.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

swift tableView.contentSize.height different iOS 13 and iOS 11 , why?

my code is
paymentTableviewHeight.constant = paymentTableView.contentSize.height
but different result iOS versions. It works correctly when I try it on ios 11 but iOS 13 is shorter.
I have fixed this problem by using below code:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableView.layoutIfNeeded()
self.tblViewHtConst.constant = self.tableView.contentSize.height
}
Calling tableView.layoutIfNeeded() UITableView will calculate contentSize again and then you can set it to height constraint.
If layoutIfNeeded() is not called before setting height constraint, it is picking up value which is set in estimatedRowHeight, in my case it was large then actual content size and blank space was visible.

Label size is not changing

I am trying to make a Chat app. I have a tableView and prototype cell in which I have UILabel. But I am stuck with label size(HxW). Please take a look in the image below:
The first label is taking extra width and height is less. I want that label should take width according to its content but till the button.
These are the contraints:
For the height I am using:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}

UITableViewAutomaticDimension not correctly sizing UIIImageView

I am trying to set the UITableViewCell height based on what is in the cell (Primarily the image). It looks fine on an iPhone 7 Plus, however any smaller devices produce something similar to this.
My code in MainViewController:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
My constraints:
What am I doing wrong? I have the height and width constraints set to 'Greater or Equal to' 200. The height and width of the UIImageView on the Storyboard are 370x370
I was able to solve this myself by doing these steps below:
Switching my Storyboard View to iPhone SE
Setting the UIImageView to 'Greater than or Equal' value: 275
Setting the aspect ratio to 1:1 for the width and height of UIImageView
Here's an image as an example:

Resources