我有一个看起来像这样的视图
----------------------------------
| [button 1] [button 2] |
----------------------------------
但我遇到一个问题,其中一个或另一个将接管全宽。我似乎无法找到压缩阻力/内容拥抱的正确组合来获得我想要的东西。
我正在使用以下可视布局代码:
H:|-(leftPadding)-[button1]-(>=middlePadding)-[button2]-(rightPadding)-|
V:|-(topPadding)-[button1]-(bottomPadding)-|
V:|-(topPadding)-[button2]-(bottomPadding)-|
填充值当前都是8.此外,按钮不应重叠,因此第二个按钮的宽度应优先于第一个按钮。
在应用中,标签可能会发生变化,所以我想看起来像:
----------------------------------
| [button 1] [some other button] |
----------------------------------
或:
----------------------------------
| [some other button] [button 2] |
----------------------------------
当我更新按钮文本时,是否还需要执行其他操作?
答案 0 :(得分:2)
对于左侧按钮(标题可能会压缩),请将压缩阻力和内容拥抱优先级保留为默认设置。换句话说,什么也不做。
对于右键(标题可能未压缩),请增加其抗压缩优先级。默认的抗压缩优先级为750.将此值增加到751就足够了。
[button2 setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal];
如果你在IB中这样做:
左键(标题可以压缩):
右键(标题无法压缩):
更新: 用于定位按钮的约束:
答案 1 :(得分:1)
[targetView setNeedsUpdateConstraints]
礼貌地请求ui更新下次运行的约束,或[targetView layoutIfNeeded]
立即强制执行约束计算。在此,更改标题后,目标视图是您的按钮。按钮上的内部UILabel如何配置为行为/调整大小将影响结果。
尝试以下内容。
#import "ADViewController.h"
@interface ADViewController ()
@property (nonatomic) UIButton *button1;
@property (nonatomic) UIButton *button2;
@end
@implementation ADViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.button1 setTitle:@"button1" forState:UIControlStateNormal];
[self.button1.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self.button1 setTag:1];
[self.button1 addTarget:self action:@selector(growTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button1];
self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.button2 setTitle:@"button2" forState:UIControlStateNormal];
[self.button2.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self.button2 setTag:2];
[self.button2 addTarget:self action:@selector(growTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button2];
}
- (void)viewWillAppear:(BOOL)animated
{
NSString *hString = @"H:|-[_button1(>=minButtonWidth)]-(>=minMiddle)-[_button2(>=minButtonWidth)]-|";
NSString *vString1 = @"V:|-[_button1]-|";
NSString *vString2 = @"V:|-[_button2]-|";
NSDictionary *metrics = @{@"minMiddle": @40,
@"minButtonWidth": @100 };
NSDictionary *views = NSDictionaryOfVariableBindings(_button1,_button2);
NSMutableArray *cons = [NSMutableArray arrayWithArray:
[NSLayoutConstraint constraintsWithVisualFormat:hString
options:0
metrics:metrics
views:views]];
[cons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:vString1
options:0
metrics:metrics
views:views]];
[cons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:vString2
options:0
metrics:metrics
views:views]];
[self.view addConstraints:cons];
}
- (void)growTitle:(UIButton *)button
{
[button setTitle:[NSString stringWithFormat:@"%@-%ld", button.titleLabel.text, button.tag]
forState:UIControlStateNormal];
[button setNeedsUpdateConstraints];
}
@end