0

我有 3 个按钮内的视图。我的问题是,我如何设置resizing mask或以autolayout编程方式获得此解决方案:

小视野和大视野..

在此处输入图像描述

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

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

谢谢帮忙。。

4

1 回答 1

3

如何使用 AutoLayout 实现此目的的示例可能如下所示:

// 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];    
于 2013-09-25T13:19:58.097 回答