在 UITableViewCell 中将按钮右对齐

Align Button to the right in UITableViewCell

我想在我的自定义单元格中有一个右对齐的按钮。使用自动布局并从右侧设置水平 space 并将其垂直居中不会在我的单元格中显示 Button,因为它太靠右了。

在图片中你可以看到我的手机。但是在实际应用中并不是这样显示的

编辑:我尝试了您的回答,但这似乎不起作用。我的 TableView 在模拟器中看起来有点奇怪:

会不会是我的控制器有问题?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as EventTableViewCell

    let event = items[indexPath.row]

    cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth

    return cell
}

对该按钮执行以下操作:

  1. 设置Width and Height constraints
  2. 为单元格视图设置垂直中心约束
  3. 将horizontal/trailing(可能是10px)space设置到单元格视图的右侧

对不起,我很笨。 我的 Cell 布局一切正常。我太专注于单元格以至于忘记了向 TableView 添加约束。添加这些约束解决了这个问题。