Ios UITableViewCell中的居中约束不起作用
我试图使用约束将UITableViewCell子类中的按钮垂直居中。我的理解是,我需要在单元格的updateConstraints方法中添加一个约束。以下是我所拥有的:Ios UITableViewCell中的居中约束不起作用,ios,uitableview,Ios,Uitableview,我试图使用约束将UITableViewCell子类中的按钮垂直居中。我的理解是,我需要在单元格的updateConstraints方法中添加一个约束。以下是我所拥有的: override func updateConstraints() { // Center the play/pause button vertically let centerY = NSLayoutConstraint(item:playPauseButton,
override func updateConstraints() {
// Center the play/pause button vertically
let centerY = NSLayoutConstraint(item:playPauseButton,
attribute:.CenterY,
relatedBy:.Equal,
toItem:contentView,
attribute:.CenterY,
multiplier:1.0,
constant:0.0)
contentView.translatesAutoresizingMaskIntoConstraints = false
playPauseButton.translatesAutoresizingMaskIntoConstraints = false
contentView.addConstraint(centerY)
super.updateConstraints()
}
但这没有任何作用(按钮显示在单元格顶部)。如果您有xib或initwithframe方法,请尝试在awakefromnib中执行该操作。我在awakefromnib中尝试过,得到了相同的结果,按钮仍然位于单元格顶部。如果您有xib或initwithframe方法,请尝试在awakefromnib中执行此操作。我在awakefromnib中尝试了此操作,得到了相同的结果,按钮仍然位于单元格顶部。