ort11
ort11

Reputation: 3349

IOS Custom Table Cell does not Automatically Resize Width for Orientation Change

Have a custom table cell with it's own .XIB and .h and .m. Nothing too fancy. When the device rotates, the cell width does not change. The tables cell for index path is called, but width is not changes (say from portrait to landscape). What would be preventing this?

Don't want to hand set the frame size.

Will post code if need to, but maybe we can answer with out code on this one.

Even if starting the app in landscape also does not set the larger width for the cell.

[Addition]

Also does not work with non-custom cells.

self.tableViewSelection.autoresizesSubviews = true;
self.tableViewSelection.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = true;

XIB for the table has the resizing seemed to be setup correctly (non-IOS6 Support).

Upvotes: 12

Views: 11511

Answers (5)

codejockie
codejockie

Reputation: 10912

In Swift 4, the below works.

cell.cellUIView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

Note: cellUIView is a UIView I added to the contentView of the prototype cells.

Upvotes: 0

mdaitn
mdaitn

Reputation: 11

For swift 2, it will be:

cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

Upvotes: 1

Bruno Lemos
Bruno Lemos

Reputation: 9263

If you are overriding the layoutSubviews or another method, don't forget to call it's super:

override func layoutSubviews() {
    //...

    super.layoutSubviews()
}

Not doing this will result in the problem you are facing.

Upvotes: 8

Kirti Nikam
Kirti Nikam

Reputation: 2206

I also faced this same issue while autoresizing custom cells on rotating. After fighting I got the solution as authorize cell.contentView, because I am adding my views as subview into cell.contentview.

I used following code and my code works fine :)

cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

Upvotes: 1

matt
matt

Reputation: 535989

The cell will automatically be the same width as the table view. So what is not resizing might be the table view. You need to give it appropriate constraints (if using Autolayout, the default in iOS 6) or autoresizing mask (otherwise) so that it will resize in response to the top-level view resizing to compensate for device rotation.

Upvotes: 2

Related Questions