Tom
Tom

Reputation: 543

UITableViewCell width not expanding on rotation to landscape

I'm having trouble resizing the content of a UITableViewCell when the device rotates to landscape (and therefore view width increases).

enter image description here enter image description here

For context, this is part of a universal split view app and is only occurring on iPhone 8 in the simulator (which doesn't support split view). Later devices which do support the split view have no issue.

In my UITableViewController, translatesAutoresizingMaskIntoConstraints = false, and 'Follow Readable Width' is unchecked in IB. I have also added:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
   self.tableView.reloadData()
}

The tableView also has a custom UITableViewCell to which I've added:

override func layoutSubviews() {
   uniqueIDLeading.constant = (self.contentView.frame.width * 0.4)

   layoutIfNeeded()
}

No errors or warnings in the console. Any other ideas?

Upvotes: 0

Views: 793

Answers (3)

In my case, and I'm sure in yours, I forgot to call a super class method.

open override func layoutSubviews() {
    super.layoutSubviews()
    
    .......
}

Upvotes: 0

Zed
Zed

Reputation: 11

I have the similar issue today, but I am not using a custom cell. I solved it with the following code.

Swift:

self.tableView.autoresizingMask = .flexibleWidth

Objective-C:

self.tableView.autoresizingMask=UIViewAutoresizingFlexibleWidth;

Upvotes: 1

Tom
Tom

Reputation: 543

Eventually fixed with the following in custom UITableViewCell class.

override func layoutSubviews() {
    super.layoutSubviews()

    autoresizingMask = .flexibleWidth
    layoutIfNeeded()
}

Upvotes: 3

Related Questions