我有一个看法:
label1 和 label2 之间存在布局约束。按下按钮将文本视图中的文本添加到 label1。之后,标签 1 被调整大小以适合文本(调用 sizeToFit)。调整大小后,约束似乎不起作用:
有人知道如何使约束起作用吗?
我的代码:
@interface ViewController ()
{
CGFloat _width;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) viewDidAppear:(BOOL)animated
{
_width = self.l1.frame.size.width;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addText:(id)sender {
self.l1.text = [NSString stringWithFormat:@"%@ %@", self.l1.text, self.text.text];
self.l1.frame = CGRectMake(self.l1.frame.origin.x, self.l1.frame.origin.y, _width, 0);
[self.l1 sizeToFit];
}
@end