简体   繁体   English

IOS自定义表格单元格不会自动调整宽度以进行方向更改

[英]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. 拥有自己的.XIB和.h和.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). 表的XIB似乎已正确设置调整大小(非IOS6支持)。

If you are overriding the layoutSubviews or another method, don't forget to call it's super: 如果您要覆盖layoutSubviews或其他方法,请不要忘记将其称为超级:

override func layoutSubviews() {
    //...

    super.layoutSubviews()
}

Not doing this will result in the problem you are facing. 不这样做会导致您遇到的问题。

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. 您需要为其提供适当的约束(如果使用Autolayout,iOS 6中的默认值)或自动调整遮罩(否则),以便调整大小以响应顶层视图调整大小以补偿设备旋转。

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. 战斗结束后我得到了解决方案作为授权cell.contentView,因为我将我的视图作为子视图添加到cell.contentview中。

I used following code and my code works fine :) 我使用以下代码,我的代码工作得很好:)

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

对于swift 2,它将是:

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

In Swift 4, the below works. 在Swift 4中,以下工作原理。

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

Note: cellUIView is a UIView I added to the contentView of the prototype cells. 注意: cellUIView是我添加到原型单元格的contentViewUIView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM