我对iOS开发还比较陌生,我的约束技能还处于基础水平。我有一组这样的标签

但是,公司名称和地址第2行并不总是必需的,因此在某些情况下它们是隐藏的,但当它们被隐藏时,我不知道如何将其他标签向上推,以便它们能够填充隐藏标签创建的空间。
非常感谢你们能给出一个解决方案。
发布于 2018-03-01 07:24:45
如果您正在使用约束,那么,例如,顶部标签应该有前导、尾随和顶到superview。
现在这个标签将有它的高度取决于它的字体和文本。如果它的文本是空的,那么它的高度将是零,这基本上是你想要的。
现在,下一个标签最好有引导和尾随到superview (或它上面的标签),然后垂直间距在它之前标签。这意味着它将略低于第一个。如果第一个没有文字,它将在顶部。
现在,使用垂直偏移量约束对所有其他标签执行相同的操作。
注意,对于您正在做的事情,使用UITableView或可能的堆栈视图似乎更合适。但是如果您想要约束,那么这就是过程。
如果事情变得复杂,您也可以将约束拖到代码中并手动操作它们。例如:
self.companyNameHeightConstraint.constant = myDataMode.companyName.isEmpty == true ? 0.0 : 50.0编辑:由于不清楚是否显示字段取决于某些外部标志,或者如果这些字符串存在,则显示这些字符串,因此我将添加带有外部标志的最小表视图过程:
@interface TableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *companyName;
@property (nonatomic, strong) NSString *address1;
@property (nonatomic, strong) NSString *address2;
@property (nonatomic, readonly) BOOL isAddress2Shown;
@property (nonatomic, readonly) BOOL isCompanyNameShown;
@end
@implementation TableViewController
- (NSArray *)generateDsiaplyableStrings {
NSMutableArray *array = [[NSMutableArray alloc] init];
if(self.name) [array addObject:self.name];
if(self.lastName) [array addObject:self.lastName];
if(self.companyName && self.isCompanyNameShown) [array addObject:self.companyName];
if(self.address1) [array addObject:self.address1];
if(self.address2 && self.isAddress2Shown) [array addObject:self.address2];
return array;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self generateDsiaplyableStrings].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *items = [self generateDsiaplyableStrings];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
cell.textLabel.text = items[indexPath.row];
return cell;
}
@end发布于 2018-03-01 08:10:27
嗨,乔尔,你为什么不试试表视图单元格内的堆栈视图,然后设置
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension而stackview和tableview将为您处理所有的事情。
https://stackoverflow.com/questions/49044531
复制相似问题