简体   繁体   English

如何设置UITableViewCell子类的子视图以使用自动调整大小的蒙版?

[英]How do I setup the subviews of my UITableViewCell subclass to use autoresizing masks?

I'm trying to implement a grid (think .NET's DataGridView) for my iOS application. 我正在尝试为我的iOS应用程序实现一个网格(以.NET的DataGridView为例)。 I'm using a UITableView with a custom UITableViewCell subclass in which I have several labels representing each column of data for that row. 我正在使用带有自定义UITableViewCell子类的UITableView,其中有多个标签代表该行的每一列数据。 My question has to do with resizing the width of those labels after a device orientation change. 我的问题与更改设备方向后调整这些标签的宽度有关。 I basically want to proportionally resize them so that, for example, a column which takes up 1/3 of the grid width in portrait orientation will still take up 1/3 of the width when switched to landscape. 我基本上想按比例调整它们的大小,以使例如在纵向方向上占据网格宽度1/3的列在切换为横向时仍将占据宽度的1/3。

Right now I'm achieving this by overriding the - (void)layoutSubviews method of my UITableViewCell subclass; 现在,我通过重写UITableViewCell子类的- (void)layoutSubviews方法来实现此目标; I basically just hard code the iPad and iPhone landscape to portrait ratio. 我基本上只是硬编码iPad和iPhone的纵横比。 However, this only works if the grid itself is resized proportionally on an orientation change, which is certainly not always the case. 但是,这仅在网格本身根据方向更改按比例调整大小时才有效,这肯定并非总是如此。

To achieve a solution that will work in any case, I thought about using self.bounds.size.width in the layoutSubviews method, but it seems as though autoresizing masks are the better solution. 为了获得在任何情况下都可以使用的解决方案,我考虑过在layoutSubviews方法中使用self.bounds.size.width ,但似乎自动调整大小的遮罩似乎是更好的解决方案。 If I set each UILabel's mask to UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin 如果我将每个UILabel的蒙版设置为UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin then I should achieve the type of scaling that I'm looking for. UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin那么我应该实现所需的缩放类型。 However, what I can't figure out is how to set the initial frames of each label. 但是,我不知道如何设置每个标签的初始帧。 If I don't know the starting bounds of the cell until layoutSubviews is called, how can I arrange my subviews to begin with? 如果在调用layoutSubviews之前我不知道单元格的开始边界,如何安排子视图的开头?

Try using self.contentView.bounds.size as the base for your calculations. 尝试使用self.contentView.bounds.size作为计算基础。 For example: 例如:

int numberOfLabels = 3;
CGFloat marginHorizontal = 10;
CGFloat spacingHorizontal = 8;
CGFloat totalWidth = cell.contentView.bounds.size.width;
// That's how much is available to all labels 
CGFloat availableWidth = (totalWidth
                          - 2 * marginHorizontal
                          - (numberOfLabels - 1) * spacingHorizontal);
// That's how much each label gets - initially!
CGFloat labelWidth = availableWidth / numberOfLabels;

Don't let it bother you that the content view initially has only a width of 320. It will be resized later, and your labels with it. 不要让您烦恼,内容视图最初只有320的宽度。稍后它将被调整大小,并带有您的标签。

By the way: You should probably specify different masks for the labels at the left and the right edge: 顺便说一句:您可能应该为左右边缘的标签指定不同的掩码:

  • Left edge: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin 左边缘: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin
  • Right edge: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin 右边缘: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin

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

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