2013-09-25 42 views
0

我有3个按钮内的视图。我的问题是,我怎么能设置resizing maskautolayout编程,我得到这个解决方案:客观c调整大小的掩码

小视图和大图..

enter image description here

Objective-C代码:

_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.. 

如何根据视图高度设置按钮高度灵活?

感谢您的帮助..

+0

你可能使用自动版式该会过得更好。 – ipmcc

+0

是的..我如何解决使用自动布局编程的问题? – q0re

回答

3

的你怎么可能做到这一点使用自动版式可能是这样一个例子:

// 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];  
+0

很好用,非常感谢;-) – q0re

+0

我可以在哪里设置按钮之间的边距? – q0re

+1

这将是视觉格式字符串的一部分。因此,不是'V:| - [_ button1] - [_ button2] - [_ button3] - |'你可以做'V:| - [_ button1] - (20) - [_ button2] - (20) - [_ button3] - |' – ipmcc