How to reposition UILabels and UIImageViews whose dimensions change dynamically?
I have a UITableView
with many rows that push a DetailViewController
depending on the selection. This detail view contains a UIImageView
, and several UILabels
(let's say book description, book commen开发者_开发问答ts etc). In viewWillAppear
I assign the proper content to these outlets, in order to display content for the selected row. My problem is that the text might be one sentence, or a couple of paragraphs. So, if I have one outlet for book description and one other for book comments, how am I supposed to position these components properly, so that their horizontal distance is always the same? I thought of creating a UIWebView
and load there dynamically generated HTML, but there has to be a cleaner way, right??
Here is a visual something if that helps :)
------------------------------
| Description |
| [multiple lines go here] |
| |
| [Image] |
| |
| Comments |
| [multiple lines go here] |
------------------------------
When you layout your UITableView, If you're using UILabels for the description and comments you can figure out the size with something like this:
CGSize labelSize = [nameLabel.text sizeWithFont: nameLabel.font
constrainedToSize: CGSizeMake( actualWidth, MAXFLOAT )
lineBreakMode: UILineBreakModeWordWrap];
Once you know the height for Description lets say, you can position the UIImageView appropriately using CGRect, etc, etc. Then calculate the height of the UITableView row with something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Create a custom view class and perform layout in -layoutSubviews using -sizeThatFits: and -sizeToFit.
Layout code does not belong in the view controller.
精彩评论