12

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).

ort11
  • 3,359
  • 4
  • 36
  • 69
  • Some screen shots would be helpful. It's hard to tell here what's really not being resized. – matt May 13 '13 at 18:22
  • I agree, but this question no longer has interest to me right now. We can remove it if moderator wants to. – ort11 May 23 '13 at 17:41

5 Answers5

8

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.

Bruno Lemos
  • 8,847
  • 5
  • 40
  • 51
2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yep, that is was is supposed to happen. For some reason in this project it is not and trying to figure it out. Tried above hard code with no luck. I assume that the correct size of the cell is set by the table view after (cell for index) and before (cell will display). – ort11 May 13 '13 at 18:09
1

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;
Kirti Nikam
  • 2,166
  • 2
  • 22
  • 43
  • What you mentioned is proper, but autoresizing masks should be set like this: (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight). I too had the same doubt as you mentioned in your code, so I posted a question in SO and you can view it for your reference: http://stackoverflow.com/questions/7754851/autoresizing-masks-programmatically-vs-interfact-builder-xib-nib – Raj Pawan Gumdal Jul 30 '14 at 06:51
  • This code no longer works as of Swift2. All of those constants end up unresolved identifiers. – zeeple Oct 28 '15 at 05:25
1

For swift 2, it will be:

cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
mdaitn
  • 11
  • 1
0

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.

codejockie
  • 9,020
  • 4
  • 40
  • 46