Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell中的居中约束不起作用_Ios_Uitableview - Fatal编程技术网

Ios UITableViewCell中的居中约束不起作用

Ios UITableViewCell中的居中约束不起作用,ios,uitableview,Ios,Uitableview,我试图使用约束将UITableViewCell子类中的按钮垂直居中。我的理解是,我需要在单元格的updateConstraints方法中添加一个约束。以下是我所拥有的: override func updateConstraints() { // Center the play/pause button vertically let centerY = NSLayoutConstraint(item:playPauseButton,

我试图使用约束将UITableViewCell子类中的按钮垂直居中。我的理解是,我需要在单元格的updateConstraints方法中添加一个约束。以下是我所拥有的:

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中尝试了此操作,得到了相同的结果,按钮仍然位于单元格顶部。