我该如何调整3 UIButtons到UITableCellView的中心? [英] How can I align 3 UIButtons to the center of an UITableCellView?

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

问题描述

我该如何调整3 的UIButton s到一个中心 UITableCellView

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

例如说我有3 的UIButton s的标题:

For example say I have 3 UIButtons with titles:


  1. 电子邮件

  2. 电话

  3. 的Skype

有可能的一个或多个的的UIButton s到被隐藏。例如,如果电话的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.

推荐答案

测试对于x code 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)作为按钮两侧的约束将是

3) constraints for buttons on either side will be

在这里输入的形象描述

如果你需要隐藏任何按钮即可使其宽度约束恒为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(没有这一切显然头痛)来实现,但是在iOS 9.0之前,将不能运行。

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的回购测试项目

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

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