xcode Interface Builder 不断重置我的自定义 UITableViewCell 的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14375552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:32:31  来源:igfitidea点击:

Interface Builder keeps resetting the width of my custom UITableViewCell

objective-cxcodeipadinterface-builder

提问by Oliver Pearmain

I'm using Interface Builder to design a custom UITableViewCell for an iPad application.

我正在使用 Interface Builder为 iPad 应用程序设计自定义 UITableViewCell 。

I have a dedicated XIB file which has a UITableViewCell as its root view. I am able to change the width of the UITableViewCell to 1024 (in the Size inspector on the right properties pane of XCode) and this DOES resize the cell on the canvas temporarily.

我有一个专用的 XIB 文件,它有一个 UITableViewCell 作为它的根视图。我可以将 UITableViewCell 的宽度更改为 1024(在 XCode 右侧属性窗格的“大小”检查器中),这会暂时调整画布上单元格的大小。

However if afterwards I try to move/resize certain items within the UITableViewCell or close and reopen the XIB, XCode decides to reset the UITableViewCell's width back to 320 (the iPhone specific width).

但是,如果之后我尝试在 UITableViewCell 中移动/调整某些项目的大小或关闭并重新打开 XIB,XCode 决定将 UITableViewCell 的宽度重置回 320(iPhone 特定宽度)。

The cell on the canvas then obscures the items within the cell and makes designing the cell impossible unless I resize the cell again. Once you've gone round this loop a few times it gets rather annoying.

然后画布上的单元格会遮住单元格内的项目,除非我再次调整单元格的大小,否则无法设计单元格。一旦你绕过这个循环几次,它就会变得很烦人。

Anyone know how I might set the width so its permanent or some how specify that the UITableViewCell is iPad specific?

任何人都知道我如何设置宽度使其永久或某些如何指定 UITableViewCell 是 iPad 特定的?



To recreate...

要重新创建...

  • File > New > File...
  • UserInterface > Empty
  • Device Family == iPad

  • Drag a new UITableViewCell onto the canvas (its created with a width of 320 despite selecting iPad previously)

  • Resize the UITableViewCell's width to 1024 (all good so far)
  • Drag and drop a UILabel onto the left side of the UITableViewCell (still good)
  • Resize the UILabel in any way by dragging a corner (DO NOT USE THE ATTRIBUTES INSPECTOR) - AND... The UITableViewCell's size is reset back to 320
  • 文件 > 新建 > 文件...
  • 用户界面 > 空
  • 设备系列 == iPad

  • 将一个新的 UITableViewCell 拖到画布上(尽管之前选择了 iPad,它还是以 320 的宽度创建)

  • 将 UITableViewCell 的宽度调整为 1024(到目前为止一切都很好)
  • 将一个 UILabel 拖放到 UITableViewCell 的左侧(仍然很好)
  • 通过拖动一个角以任何方式调整 UILabel 的大小(不要使用属性检查器) - 而且...... UITableViewCell 的大小被重置回 320


EDIT:

编辑:

I have just received the following response from Apple to the Radar ticket I created ages ago...

我刚刚收到 Apple 对我很久以前创建的 Radar 票的以下回复......

Hello Oliver,

你好奥利弗,

This is a follow-up regarding regarding Bug ID# 13222753.

Engineering has requested the following information in order to further investigate this issue:

We believe this issue has been addressed in the latest Xcode 5.1 Seed.

This is a pre-release version of the Xcode developer toolset for Mac, iPhone, and iPad. It includes the Xcode IDE, iOS Simulator, and all required tools and frameworks for building OS X and iOS apps. Please test with this release, and update your bug report with the results.

Xcode 5.1 Seed - Build 5B71f Posted Date: Dec 13, 2013

这是关于错误 ID# 13222753 的后续行动。

工程已要求提供以下信息以进一步调查此问题:

我们相信这个问题已经在最新的 Xcode 5.1 Seed 中得到解决。

这是适用于 Mac、iPhone 和 iPad 的 Xcode 开发人员工具集的预发布版本。它包括 Xcode IDE、iOS 模拟器以及构建 OS X 和 iOS 应用程序所需的所有工具和框架。请使用此版本进行测试,并使用结果更新您的错误报告。

Xcode 5.1 Seed - Build 5B71f 发布日期:2013 年 12 月 13 日

I've also just tried to recreate the problem in Xcode 5.0.2 and it looks like its fixed with that build also.

我也只是尝试在 Xcode 5.0.2 中重现这个问题,看起来它也用那个版本修复了。

采纳答案by Edward Huynh

I don't know if you consider this a valid answer, but the problem only seemed to occur for me when I was using autolayoutin IB. If I turn that off, the problem of Xcode resizing the UITableViewCell goes away.

我不知道你是否认为这是一个有效的答案,但这个问题似乎只发生在我在 IB中使用自动布局时。如果我关闭它,Xcode 调整 UITableViewCell 大小的问题就会消失。

To turn off autolayout, just select the UITableViewCell and then on the right in the 'File inspector', there is a checkbox for autolayoutto turn it off.

要关闭autolayout,只需选择 UITableViewCell ,然后在“文件检查器”右侧,有一个自动布局复选框将其关闭。

回答by phatmann

The workaround is to not add a Table View Cell to your nib. Instead, use a regular View to contain your subviews.

解决方法是不要向您的笔尖添加表格视图单元格。相反,使用常规视图来包含您的子视图。

Create a custom UITableViewCell class that loads the nib and sets the top-level nib view as the subview of the cell content view:

创建一个自定义 UITableViewCell 类来加载笔尖并将顶级笔尖视图设置为单元格内容视图的子视图:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        UINib *nib = [UINib nibWithNibName:@"YourNibName" bundle:nil];
        UIView* containerView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
        containerView.translatesAutoresizingMaskIntoConstraints = NO
        [self.contentView addSubview:containerView];

        NSDictionary *views = NSDictionaryOfVariableBindings(containerView);
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[containerView]|" options:0 metrics:nil views:views]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:views]];
    }

    return self;
}

Register your class with the table view:

使用表格视图注册您的课程:

[tableView registerClass:YourCellClass forCellReuseIdentifier:@"YourCellID"]`

回答by SAE

I just did a test and had no problems resizing the cell. It did make no difference, if the Xib was created with the 'for iPhone' or 'for iPad' option. So, this works as expected.

我刚刚做了一个测试,调整单元格大小没有问题。如果 Xib 是使用“for iPhone”或“for iPad”选项创建的,那并没有什么区别。所以,这按预期工作。

The trick is, not to drag the size but, to use the Inspector to change the width - this leaves the cell at it's size of 1024.

诀窍是,不要拖动大小,而是使用 Inspector 更改宽度 - 这使单元格的大小为 1024。

回答by Raj Pawan Gumdal

I confirm that Edward Huynh's answeris correct. At the same time, I could not have afforded to turn off autolayout, change the height, turn the autolayouts back again. This would require me to redo all autolayout settings for each subview of the content view.

我确认Edward Huynh 的回答是正确的。同时,我无法关闭自动布局,更改高度,再次将自动布局转回来。这将需要我为内容视图的每个子视图重做所有自动布局设置。

I was just experimenting and found out that opening xib file in TextEdit and changing the width & height of the tableViewCell and the tableViewCellContentView will actually retain all autolayouts and the size of the cell is updated. Few of the constraints' constant values will now be obsolete which can easily be fixed by using "update constraints" on such constraints.

我只是在试验,发现在 TextEdit 中打开 xib 文件并更改 tableViewCell 和 tableViewCellContentView 的宽度和高度实际上会保留所有自动布局,并且单元格的大小会更新。现在很少有约束的常量值会过时,可以通过对此类约束使用“更新约束”轻松修复。

enter image description here

在此处输入图片说明