ios - 如何将 3 个 UIButton 对齐到 UITableCellView 的中心?

标签 ios swift uitableview uibutton autolayout

如何将 3 个 UIButton 对齐到 UITableCellView 的中心?

例如,假设我有 3 个带有标题的 UIButton:

  1. 电子邮件
  2. 电话
  3. 网络电话

有可能隐藏一个或多个 UIButton。例如,如果电话 UIButton 被隐藏,那么只有电子邮件和 Skype 应该居中对齐。如果电话和 Skype 被隐藏,那么只有电子邮件应该居中对齐。

当任何 UIButton 被隐藏时,可见的应该居中对齐。

我想将它们水平和垂直居中。

最佳答案

已针对 xcode 7 进行测试:

我想你正在寻找类似的东西

enter image description here

解决方案:

enter image description here

步骤: 1) 需要的是一个封装 View ,它将所有三个按钮(Skype、电话、电子邮件)置于中心,而不管其中是否有一个、两个或三个按钮。为此,创建了一个持有人 View ,在下面的快照中以绿色背景显示。 enter image description here

持有者观点的约束是

enter image description here

它只是为了容纳所有的 subview ,它会从它的内容中获取它的大小,所以不需要给它高度/宽度限制。

2) 现在中心按钮的约束将是

enter image description here

3) 两侧按钮的约束将是

enter image description here

如果您需要隐藏任何按钮,只需将其宽度约束常量设置为 0,所有其他按钮将相应地重新排列

对于 TableView 单元格:

    @IBOutlet weak var emailButtonWidthConstraint : NSLayoutConstraint?
    @IBOutlet weak var phoneButtonWidthConstraint : NSLayoutConstraint?
    @IBOutlet weak var skypeButtonWidthConstraint : NSLayoutConstraint?

    func showButtons(showSkype showSkype : Bool, showEmail : Bool, showPhone : Bool ){
        emailButtonWidthConstraint?.constant = showEmail ? 54.0 : 0.0
        phoneButtonWidthConstraint?.constant = showPhone ? 54.0 : 0.0
        skypeButtonWidthConstraint?.constant = showSkype ? 54.0 : 0.0

        self.layoutIfNeeded()
    }

使用:

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

        if indexPath.row == 0 {
            cell?.showButtons(showSkype: true, showEmail: true, showPhone: true)
        } else if indexPath.row == 1 {
            cell?.showButtons(showSkype: false, showEmail: true, showPhone: true)
        } else if indexPath.row == 2 {
            cell?.showButtons(showSkype: true, showEmail: false, showPhone: true)
        } else if indexPath.row == 3 {
            cell?.showButtons(showSkype: true, showEmail: true, showPhone: false)
        } else if indexPath.row == 4 {
            cell?.showButtons(showSkype: false, showEmail: false, showPhone: true)
        } else if indexPath.row == 5 {
            cell?.showButtons(showSkype: false, showEmail: true, showPhone: false)
        } else if indexPath.row == 6 {
            cell?.showButtons(showSkype: true, showEmail: false, showPhone: false)
        } else {
            cell?.showButtons(showSkype: true, showEmail: true, showPhone: true)
        }

        return cell!
    }

同样可以用 UIStackView 实现(显然没有所有这些令人头疼的事情)但是不会在 9.0 之前的 iOS 上运行

更新(2015 年 10 月 26 日):

GitHub Repo用于测试项目

关于ios - 如何将 3 个 UIButton 对齐到 UITableCellView 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016648/

相关文章:

iOS 8 youtube 视频嵌入

ios - 自定义字体iOS不显示

ios - 通过 ActivityViewController iOS swift 共享应用程序链接?

ios - UITextField NSForegroundColor 在屏幕旋转 Swift 后变为透明

iphone - UITableView 滚动缓慢,内存泄漏问题

iphone - UIImage encodeWithCoder 识别选择器?

ios - CGRect 在 Swift 中改变原点

ios - 在 Swift 中以编程方式获取包标识符?

swift - 样本缓冲区的媒体类型必须匹配接收方的媒体类型 ("soun")

ios - 如何在当前 View Controller 中重新加载另一个 uiviewcontroller 的 TableView