Basic iOS auto layout usage. See Adopting Auto Layout and Visual Format language for reference.
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Disable autoresizing mask translation for parent.
self.translatesAutoresizingMaskIntoConstraints = NO;
_subview1 = [[UIView alloc] init];
// Disable autoresizing mask translation for subview.
_subview1.translatesAutoresizingMaskIntoConstraints = NO;
_subview1.backgroundColor = [UIColor redColor];
[self addSubview:_subview1];
// Creates a dictionary of bindings to be used in visual format.
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(_subview1);
// H: horizontal layout
// |-50- spacing in relation to superview
// [_subview1(==50)] subview1's width
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[_subview1(==50)]"
options:0
metrics:nil
views:viewBindings]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_subview1(==50)]"
options:0
metrics:nil
views:viewBindings]];
}
return self;
}