uitableViewCell accessoryView 中的 UILabel 向上移动
UILabel as tableViewCell's accessoryView is shifted up
我正在创建一个普通的 UILabel 并将其设置为 tableViewCell 的 accessoryView。我的理解是,accessoryView 在单元格内保持垂直居中对齐。但这并没有发生。当我减小标签文本的字体时,附件视图会向上移动更多。按钮工作正常。 UILabel 问题。
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello"
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 21))
label.backgroundColor = UIColor.red
label.font = UIFont(name: "Helvetica Neue", size: 12)
label.highlightedTextColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.green
label.textAlignment = .right;
label.clipsToBounds = true
label.autoresizesSubviews = true
label.contentMode = .left
label.text = "123";
cell.accessoryView = label
return cell
}
如果您使用的是 SwiftUI 之前的版本,则应考虑使用自动布局将 UILabel 居中放置在单元格中,以防止它向上移动。如果您使用的是 SwiftUI,则必须使用 SwiftUI 布局系统。希望对您有所帮助。
我找到了解决办法。我给 UILabel 的框架是 CGRect(x: 0, y: 0, width: 42, height: 21)。如果我给出与文本大小相同的高度,即 12.0,标签将居中对齐 :)。
这似乎是一个 iOS 13 问题。在 iOS 12 台设备中未出现。
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 12))
正如@Faizyy 所注意到的,iOS 13 存在一个问题,但是提供较小的高度并不总是一种选择。我的方法是将 UILabel 放入与 UILabel 大小相同的 UIView 中,并将该容器视图用作附属视图。这为我解决了对齐问题。这是一个例子:
cell.accessoryView = [self getBadge:unread];
-(UIView *)getBadge:(int)count {
CGFloat size = 28;
CGFloat digits = [[@(count) stringValue] length];
CGFloat width = MAX(size, 0.7 * size * digits);
UILabel *badge = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, size)];
badge.text = [@(count) stringValue];
badge.layer.cornerRadius = size / 2;
badge.layer.masksToBounds = YES;
badge.textAlignment = NSTextAlignmentCenter;
badge.font = [UIFont fontWithName:@"AvenirNext-Medium" size:16];
badge.textColor = UIColor.whiteColor;
badge.backgroundColor = VSColor.blue;
UIView *badgeHolder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, size)];
badgeHolder.backgroundColor = UIColor.clearColor;
[badgeHolder addSubview:badge];
return badgeHolder;
}
我正在创建一个普通的 UILabel 并将其设置为 tableViewCell 的 accessoryView。我的理解是,accessoryView 在单元格内保持垂直居中对齐。但这并没有发生。当我减小标签文本的字体时,附件视图会向上移动更多。按钮工作正常。 UILabel 问题。
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello"
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 21))
label.backgroundColor = UIColor.red
label.font = UIFont(name: "Helvetica Neue", size: 12)
label.highlightedTextColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.green
label.textAlignment = .right;
label.clipsToBounds = true
label.autoresizesSubviews = true
label.contentMode = .left
label.text = "123";
cell.accessoryView = label
return cell
}
如果您使用的是 SwiftUI 之前的版本,则应考虑使用自动布局将 UILabel 居中放置在单元格中,以防止它向上移动。如果您使用的是 SwiftUI,则必须使用 SwiftUI 布局系统。希望对您有所帮助。
我找到了解决办法。我给 UILabel 的框架是 CGRect(x: 0, y: 0, width: 42, height: 21)。如果我给出与文本大小相同的高度,即 12.0,标签将居中对齐 :)。 这似乎是一个 iOS 13 问题。在 iOS 12 台设备中未出现。
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 12))
正如@Faizyy 所注意到的,iOS 13 存在一个问题,但是提供较小的高度并不总是一种选择。我的方法是将 UILabel 放入与 UILabel 大小相同的 UIView 中,并将该容器视图用作附属视图。这为我解决了对齐问题。这是一个例子:
cell.accessoryView = [self getBadge:unread];
-(UIView *)getBadge:(int)count {
CGFloat size = 28;
CGFloat digits = [[@(count) stringValue] length];
CGFloat width = MAX(size, 0.7 * size * digits);
UILabel *badge = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, size)];
badge.text = [@(count) stringValue];
badge.layer.cornerRadius = size / 2;
badge.layer.masksToBounds = YES;
badge.textAlignment = NSTextAlignmentCenter;
badge.font = [UIFont fontWithName:@"AvenirNext-Medium" size:16];
badge.textColor = UIColor.whiteColor;
badge.backgroundColor = VSColor.blue;
UIView *badgeHolder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, size)];
badgeHolder.backgroundColor = UIColor.clearColor;
[badgeHolder addSubview:badge];
return badgeHolder;
}