Using autolayout in a UI table view section header view
Using autolayout in a UI table view section header view
- Subject: Using autolayout in a UI table view section header view
- From: Rick Mann <email@hidden>
- Date: Thu, 11 Jul 2013 14:26:32 -0700
I'm trying to reproduce the UITableView section header view used by iOS for regular text headers. Based on Reveal.app, I see that it's a view sized 540 x 34 that contains a UILabel (subclass) sized width x 21, positioned 40x5.
I wanted to add a UIActivityIndicator just to the end of the text label, so I built up the same hierarchy in IB:
http://cl.ly/image/1L3q1b43471w
But that alone wasn't enough; iOS stretches my view to the full width of the table, and squashes it vertically to something very short. So I tried adding a height constraint in code, because IB doesn't let me add a height constraint to a top-level view:
+ (UITableViewSectionHeaderWithActivityIndicator*)
instance
{
static UINib* sNib;
static dispatch_once_t sInit;
dispatch_once(&sInit,
^{
sNib = [UINib nibWithNibName: @"UITableViewSectionHeaderWithActivityIndicator" bundle: nil];
});
NSArray* objs = [sNib instantiateWithOwner: nil options: nil];
UITableViewSectionHeaderWithActivityIndicator* v = objs.lastObject;
// IB won't let us add constraints to the top-level view…
//v.translatesAutoresizingMaskIntoConstraints = false;
NSDictionary* metrics = @{ @"height" : @(v.frame.size.height) };
NSDictionary* views = @{ @"view" : v };
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat: @"V:[view(height)]" options: 0 metrics: metrics views: views];
[v addConstraints: constraints];
return v;
}
This fails with:
> 2013-07-11 14:21:32.317 App[70432:c07] Unable to simultaneously satisfy constraints.
> Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
> (
> "<NSLayoutConstraint:0x9fb6570 V:[UITableViewSectionHeaderWithActivityIndicator:0x9fb6c80(34)]>",
> "<NSAutoresizingMaskLayoutConstraint:0x9fb50d0 h=--& v=--& V:[UITableViewSectionHeaderWithActivityIndicator:0x9fb6c80(10)]>"
> )
>
> Will attempt to recover by breaking constraint
> <NSLayoutConstraint:0x9fb6570 V:[UITableViewSectionHeaderWithActivityIndicator:0x9fb6c80(34)]>
So I tried setting translatesAutoresizingMaskIntoConstraints to false, but that results in
> 2013-07-11 14:24:53.688 App[70482:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776
> 2013-07-11 14:24:57.127 App[70482:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
So I commented out that line and then added the bottom space constraint in IB (between the label and the container view, to force the view's height).
Any suggestions?
Thanks,
--
Rick
_______________________________________________
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