【发布时间】:2013-02-24 23:46:16
【问题描述】:
我认为下面的代码应该会导致一个多行标签,然后是一个按钮,但是,在布局之后,只有一行标签显示出来。虽然我可以在垂直布局中放置一个明确的高度,但这会破坏目的。关于我应该应用哪些其他限制的任何想法?
UILabel *lbExplain = [[UILabel alloc] init];
lbExplain.text = @"The Sync process allows you to synchronize your library between different devices. By clicking on the button below you can find other devices to sync with. The other device also has to be running this applicaton.";
lbExplain.lineBreakMode = NSLineBreakByWordWrapping;
lbExplain.numberOfLines = 0;
lbExplain.translatesAutoresizingMaskIntoConstraints = NO;
UIButton *btnPartner = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnPartner setTitle:@"Look for Partners" forState:UIControlStateNormal];
[btnPartner addTarget:self action:@selector(findPartners:) forControlEvents:UIControlEventTouchUpInside];
btnPartner.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:lbExplain];
[self.view addSubview:btnPartner];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[lbExplain]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lbExplain)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[lbExplain]-[btnPartner]" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(lbExplain, btnPartner)]];
【问题讨论】:
-
如果你在标签上调用
-sizeToFit会发生什么? -
我无法立即看到缺少的东西。如果你在 IB 中做基本相同的事情,它就可以正常工作。
标签: ios autolayout