简体   繁体   中英

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. I'm using a UITableView with a custom UITableViewCell subclass in which I have several labels representing each column of data for that row. 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.

Right now I'm achieving this by overriding the - (void)layoutSubviews method of my UITableViewCell subclass; I basically just hard code the iPad and iPhone landscape to portrait ratio. 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. If I set each UILabel's mask to UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleLeftMargin | UIViewAutoResizingFlexibleRightMargin then I should achieve the type of scaling that I'm looking for. 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?

Try using self.contentView.bounds.size as the base for your calculations. 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.

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
  • Right edge: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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