【问题标题】:objective c resizing mask目标 c 调整大小蒙版
【发布时间】:2013-09-25 21:19:58
【问题描述】:

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

小视图和大视图..

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

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

感谢您的帮助..

【问题讨论】:

  • 你可能最好使用 AutoLayout。
  • 是的..我如何以编程方式使用自动布局解决问题?

标签: objective-c autoresizingmask


【解决方案1】:

如何使用 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];    

【讨论】:

  • 在哪里可以设置按钮之间的边距?
  • 这将是可视格式字符串的一部分。所以你可以用V:|-[_button1]-(20)-[_button2]-(20)-[_button3]-|代替V:|-[_button1]-[_button2]-[_button3]-|
猜你喜欢
  • 2022-07-20
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 2011-01-31
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多