Created
August 10, 2013 19:43
-
-
Save mayoff/6201864 to your computer and use it in GitHub Desktop.
Untested UIView subclass to be used as the custom class of a container view in a storyboard, to make that container view honor the intrinsic content size of its embedded content view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Use this as the custom class of a container view in a storyboard if you want the container view to honor its embedded view's intrinsic content size. | |
| */ | |
| @interface RobIntrinsicContentSizeHonoringContainerView : UIView | |
| /** Connect this to my width constraint in the storyboard if you want me to use my subview's intrinsic content width. */ | |
| @property (nonatomic, strong) IBOutlet NSLayoutConstraint *widthConstraint; | |
| /** Connect this to my height constraint in the storyboard if you want me to use my subview's intrinsic content height. */ | |
| @property (nonatomic, strong) IBOutlet NSLayoutConstraint *heightConstraint; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "RobIntrinsicContentSizeHonoringContainerView.h" | |
| @implementation RobIntrinsicContentSizeHonoringContainerView | |
| - (void)addSubview:(UIView *)subview { | |
| subview.translatesAutoresizingMaskIntoConstraints = NO; | |
| [super addSubview:subview]; | |
| NSDictionary *views = NSDictionaryOfVariableBindings(subview); | |
| if (self.widthConstraint) { | |
| [self removeConstraint:self.widthConstraint]; | |
| self.widthConstraint = nil; | |
| [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview]|" options:0 metrics:nil views:views]]; | |
| } | |
| if (self.heightConstraint) { | |
| [self removeConstraint:self.heightConstraint]; | |
| self.heightConstraint = nil; | |
| [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subview]|" options:0 metrics:nil views:views]]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not just: