Question

i have a view within 3 Buttons. My problem is, how can i set the resizing mask or autolayout programmatically that i get this solution:

Small view and a larger view..

enter image description here

Objective-c code:

_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)]; // set a height?!
[_button1 setBackgroundColor:[UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f]];
_button1.autoresizingMask = UIViewAutoresizingFlexibleHeight;

_button2 and_button3 are the same..

How can i set the button height flexible in dependence of the view height?

Thanks for helping..

Was it helpful?

Solution

An example of how you might achieve this using AutoLayout might look like this:

// Build the view hierarchy
_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button1 setBackgroundColor:[UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f]];
_button1.translatesAutoresizingMaskIntoConstraints = NO;
_button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button2 setBackgroundColor:[UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f]];
_button2.translatesAutoresizingMaskIntoConstraints = NO;
_button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button3 setBackgroundColor:[UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f]];
_button3.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview: _button1];
[self.view addSubview: _button2];
[self.view addSubview: _button3];

NSMutableArray* constraints = [NSMutableArray array];

// Arrange them vertically
[constraints addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-[_button1]-[_button2]-[_button3]-|" options:0 metrics:nil views: NSDictionaryOfVariableBindings(_button1, _button2, _button3)]];

// Make their heights equal
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: _button2 attribute: NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: _button3 attribute: NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];

// Set their widths equal to superview
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];

// Center them horizontally in the superview
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[constraints addObject: [NSLayoutConstraint constraintWithItem:_button3 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

// Add the constraints
[self.view addConstraints: constraints];    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top