Swift:封装的布局高度始终比视图大 .333
Swift: Encapsulated Layout Height always .333 larger than view
我正在构建一个包含可变数量单元格的 TableView。其中一种类型的单元格应具有 69.0 的固定高度,但 xcode 一直将其封装的布局高度设置为 .333 大于我设置的高度,这意味着它打破了约束,因为封装的布局高度为 69.333。例如,如果我将 Cell 的大小更改为 70,则约束设置为 70.333。我不明白是什么原因造成的。
这是单元格:
class AnnotationCell: UITableViewCell {
//Set identifier to be able to call it later on
static let identifier = "AnnotationCell"
var annotation: Annotation!
//MARK: - Configure
public func configure(annotation: Annotation) {
self.annotation = annotation
}
//MARK: - Cell Style
//Add all subviews in here
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
//Customize Cell
contentView.backgroundColor = colors.darkGray
contentView.layer.borderWidth = 0
//Favorite
let favoriteView = UIView()
favoriteView.backgroundColor = colors.gray
favoriteView.addBorders(edges: [.top], color: colors.lightGray, width: 1)
favoriteView.translatesAutoresizingMaskIntoConstraints = false
let favIcon = UIImageView()
let myEmoji = "".textToImage() //Test
favIcon.image = myEmoji
favIcon.contentMode = .scaleAspectFill
favIcon.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(favIcon)
let stringStack = UIStackView()
stringStack.axis = .vertical
stringStack.spacing = 6
stringStack.translatesAutoresizingMaskIntoConstraints = false
let titleString = UILabel()
titleString.text = "Test"
titleString.textColor = colors.justWhite
titleString.font = UIFont(name: "montserrat", size: 17)
titleString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(titleString)
let addressString = UILabel()
addressString.text = "Test 2"
addressString.textColor = colors.lightGray
addressString.font = UIFont(name: "montserrat", size: 14)
addressString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(addressString)
favoriteView.addSubview(stringStack)
let editButton = UIButton()
editButton.tintColor = colors.lightGray
let mediumConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold, scale: .medium)
let mediumEditButton = UIImage(systemName: "chevron.right", withConfiguration: mediumConfig)
editButton.setImage(mediumEditButton, for: .normal)
editButton.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(editButton)
contentView.addSubview(favoriteView)
//Define Constraints
NSLayoutConstraint.activate([
favoriteView.heightAnchor.constraint(equalToConstant: 69),
favoriteView.topAnchor.constraint(equalTo: contentView.topAnchor),
favoriteView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
favoriteView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
favoriteView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
favIcon.leadingAnchor.constraint(equalTo: favoriteView.leadingAnchor, constant: 15),
favIcon.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
favIcon.heightAnchor.constraint(equalToConstant: 50),
favIcon.widthAnchor.constraint(equalToConstant: 50),
stringStack.leadingAnchor.constraint(equalTo: favIcon.trailingAnchor, constant: 20),
stringStack.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: favoriteView.trailingAnchor, constant: -15),
editButton.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
])
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我根本没有在 TableView 中设置 Cell 的高度 - 我已经尝试过了,但这并没有使事情变得更好。单元格显示正确,但控制台抛出:
[LayoutConstraints] 无法同时满足约束条件。
(
“”,
“”,
“”,
“”
)
将尝试通过打破约束来恢复
我试过给每个子视图一个固定的高度(这对于这个布局中的堆栈视图没有意义)但是也没有用。我在视图(不是 TableViewCell)中有相同的布局,没有任何问题,所以我猜它必须对 TableView 做一些事情。
这是我从视图层次结构中看到的约束,以防有人注意到它。我已经开放了大部分限制以使其尽可能清楚。从红色箭头旁边可以看出,69.0 的约束似乎就在那里,我看不出有什么不同。我唯一能看到导致这个问题的是“UISysmenBackGroundView”,它里面有一个没有任何限制的视图。
1/3 pt 在 3x 设备上听起来像是细线粗细。它可能是分隔符吗?如果已启用,请尝试将其关闭以查看是否可以解决问题。
我正在构建一个包含可变数量单元格的 TableView。其中一种类型的单元格应具有 69.0 的固定高度,但 xcode 一直将其封装的布局高度设置为 .333 大于我设置的高度,这意味着它打破了约束,因为封装的布局高度为 69.333。例如,如果我将 Cell 的大小更改为 70,则约束设置为 70.333。我不明白是什么原因造成的。
这是单元格:
class AnnotationCell: UITableViewCell {
//Set identifier to be able to call it later on
static let identifier = "AnnotationCell"
var annotation: Annotation!
//MARK: - Configure
public func configure(annotation: Annotation) {
self.annotation = annotation
}
//MARK: - Cell Style
//Add all subviews in here
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
//Customize Cell
contentView.backgroundColor = colors.darkGray
contentView.layer.borderWidth = 0
//Favorite
let favoriteView = UIView()
favoriteView.backgroundColor = colors.gray
favoriteView.addBorders(edges: [.top], color: colors.lightGray, width: 1)
favoriteView.translatesAutoresizingMaskIntoConstraints = false
let favIcon = UIImageView()
let myEmoji = "".textToImage() //Test
favIcon.image = myEmoji
favIcon.contentMode = .scaleAspectFill
favIcon.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(favIcon)
let stringStack = UIStackView()
stringStack.axis = .vertical
stringStack.spacing = 6
stringStack.translatesAutoresizingMaskIntoConstraints = false
let titleString = UILabel()
titleString.text = "Test"
titleString.textColor = colors.justWhite
titleString.font = UIFont(name: "montserrat", size: 17)
titleString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(titleString)
let addressString = UILabel()
addressString.text = "Test 2"
addressString.textColor = colors.lightGray
addressString.font = UIFont(name: "montserrat", size: 14)
addressString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(addressString)
favoriteView.addSubview(stringStack)
let editButton = UIButton()
editButton.tintColor = colors.lightGray
let mediumConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold, scale: .medium)
let mediumEditButton = UIImage(systemName: "chevron.right", withConfiguration: mediumConfig)
editButton.setImage(mediumEditButton, for: .normal)
editButton.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(editButton)
contentView.addSubview(favoriteView)
//Define Constraints
NSLayoutConstraint.activate([
favoriteView.heightAnchor.constraint(equalToConstant: 69),
favoriteView.topAnchor.constraint(equalTo: contentView.topAnchor),
favoriteView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
favoriteView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
favoriteView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
favIcon.leadingAnchor.constraint(equalTo: favoriteView.leadingAnchor, constant: 15),
favIcon.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
favIcon.heightAnchor.constraint(equalToConstant: 50),
favIcon.widthAnchor.constraint(equalToConstant: 50),
stringStack.leadingAnchor.constraint(equalTo: favIcon.trailingAnchor, constant: 20),
stringStack.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: favoriteView.trailingAnchor, constant: -15),
editButton.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
])
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我根本没有在 TableView 中设置 Cell 的高度 - 我已经尝试过了,但这并没有使事情变得更好。单元格显示正确,但控制台抛出:
[LayoutConstraints] 无法同时满足约束条件。
(
“
将尝试通过打破约束来恢复
我试过给每个子视图一个固定的高度(这对于这个布局中的堆栈视图没有意义)但是也没有用。我在视图(不是 TableViewCell)中有相同的布局,没有任何问题,所以我猜它必须对 TableView 做一些事情。
这是我从视图层次结构中看到的约束,以防有人注意到它。我已经开放了大部分限制以使其尽可能清楚。从红色箭头旁边可以看出,69.0 的约束似乎就在那里,我看不出有什么不同。我唯一能看到导致这个问题的是“UISysmenBackGroundView”,它里面有一个没有任何限制的视图。
1/3 pt 在 3x 设备上听起来像是细线粗细。它可能是分隔符吗?如果已启用,请尝试将其关闭以查看是否可以解决问题。