Re: is this possible in iOS with constraints?
Re: is this possible in iOS with constraints?
- Subject: Re: is this possible in iOS with constraints?
- From: Matt Neuburg <email@hidden>
- Date: Fri, 02 Nov 2012 19:11:22 -0700
Aaaaaand heeeeere's the code! Oddly, I never did quite solve the problem I originally set out to solve; everything was happening correctly except that at the last minute the cell was snapping back to its original size, even though the constraints said clearly enough that it should not. However, I stumbled upon a way around that - I discovered systemLayoutSizeFittingSize:UILayoutFittingExpandedSize.
Just to remind everyone what the problem is: I want to precalculate the heights for all the cells in the table by filling those cells with their content and letting the content and the autolayout mechanism push each cell to the size it would need to assume in order to accommodate the content.
Here's how I do it. It's a simple case (the content consists of just one label) but it should work for more complex cases. The important thing is that autolayout is set up beforehand in the nib for the cell. We can then take advantage of that autolayout to size a practice cell from the inside out, as it were, thus giving us the height we will need later.
- (void)viewDidLoad {
[super viewDidLoad];
// not shown...
// ... set up "arr", the data model - array of strings, one string per cell
NSMutableArray* heights = [NSMutableArray arrayWithCapacity:[arr count]];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString* s = obj;
CGFloat h = [self cellHeightForLabelString:s];
[heights insertObject:@(h) atIndex:idx];
}];
self.heights = heights;
}
- (CGFloat) cellHeightForLabelString:(NSString*)s {
// use the autolayout mechanism to generate the cell height
NSArray* objs = [[UINib nibWithNibName:@"Cell" bundle:nil]
instantiateWithOwner:nil options:nil];
Cell* cell = objs[0];
UILabel* lab = cell.lab;
lab.text = s;
[lab sizeToFit];
return [cell systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;
}
Hope that helps someone some day - m.
On Nov 2, 2012, at 5:24 PM, Matt Neuburg <email@hidden> wrote:
> On Nov 2, 2012, at 10:41 AM, Luke Hiesterman <email@hidden> wrote:
>
>> it's not in the view hierarchy, in which case the autolayout engine won't do anything for you
>
> Okay, the good news is that this turns out to be false! It turns out that you *can* exercise the autolayout engine for any view hierarchy by sending layoutIfNeeded to it. A lucky discovery.
>
> So I can certainly make a hierarchy and lay it out with constraints and autolayout, without putting that hierarchy into the visible interface.
>
> My problem now is just that I can't seem to find the autolayout fu that will cause a view containing a label to be laid out by the label, rather than the other way round.
>
> In other words, I have a view superview and a label subview; I am resizing the label by giving it text content and saying sizeToFit; and now the label is the right size. So now I am going to exercise autolayout, and I want the view superview to resize to meet the constraints relating the superview to the label - what's happening instead is that the label is resizing again and the superview is staying the same.
>
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden