如何将 3 个 UIButtons 对齐到 UITableCellView 的中心? [英] How can I align 3 UIButtons to the center of an UITableCellView?

查看:12
本文介绍了如何将 3 个 UIButtons 对齐到 UITableCellView 的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I align 3 UIButtons to the center of an UITableCellView?

例如说我有 3 个带有标题的 UIButton:

For example say I have 3 UIButtons with titles:

  1. 电子邮件
  2. 电话
  3. Skype

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

It is possible for one or more of the UIButtons to be hidden. For example, if the Phone UIButton is hidden then only Email and Skype should be aligned in the center. If Phone and Skype is hidden then only Email should be aligned in the center.

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

When any of the UIButtons are hidden, the visible ones should be aligned in the center.

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

I want to center them both horizontally and vertically.

推荐答案

已针对 xcode 7 测试:

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

i suppose your are looking for something like that

解决方案:

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

Steps: 1) what is needed is an encapsulating view which holds all three buttons (skype, phone, email) into center irrespective of whether there is one button, two or three inside it. For that a holder view is created which is shown with green background in below snapshot.

该持有人视图的约束是

它只是保存所有的子视图,它会从它的内容中获取它的大小,所以不需要给它高度/宽度限制.

it is just to hold all the subviews, it will get its size from its content so no need to give it height/width constraints.

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

2) now constraints for the button in the center will be

3) 两侧按钮的约束将是

如果您需要隐藏任何按钮,只需将其宽度约束设置为 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 上运行

Same can be achieved with UIStackView (without all this headache obviously) but that won't run on iOS prior to 9.0

更新(2015 年 10 月 26 日):

GitHub Repo 用于测试项目

这篇关于如何将 3 个 UIButtons 对齐到 UITableCellView 的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆