Swift以编程方式创建宽度百分比

fly*_*iah 2 user-interface uibutton uitableview ios swift

我正在以编程方式在Swift中为UITableViewCell创建多个按钮,并且希望将它们的宽度设置为UITableViewCell的特定百分比宽度,以便它们耗尽所有空间.现在我在for循环中创建按钮(我希望能够创建可变数量的按钮)并且正在使用

button.buttonWidth = self.contentView.frame.width / numberOfButtons
Run Code Online (Sandbox Code Playgroud)

其中button是UIButton,self是UITableViewCell,numberOfButtons显然是按钮的数量.我也尝试过:

button.buttonWidth = self.frame.size.width / numberOfButtons
Run Code Online (Sandbox Code Playgroud)

以及使用/不使用.size和使用/不使用contentView的每个组合.然而,这些似乎不起作用,因为按钮太大了.有谁知道怎么做到这一点?

注意:我不能在故事板中使用Autolayout,因为我正在创建可变数量的按钮.

Rob*_*Rob 5

你说:

注意:我不能在故事板中使用Autolayout,因为我正在创建可变数量的按钮.

您无法在Interface Builder中添加可变数量的按钮,但没有什么可以阻止您使用autolayout.您需要设置的基本自动布局限制是:

  • 将按钮设置为具有相同的宽度;
  • 设置按钮使其前导约束连接到前一个按钮的尾随约束;
  • 第一个按钮应该将其前导约束连接到容器视图; 和
  • 最后一个按钮也应该将其尾部约束连接到容器.

因此,例如,在Swift 3中,您可以执行以下操作:

var previousButton: UIButton?
for _ in 0 ..< count {
    let button = ...
    button.translatesAutoresizingMaskIntoConstraints = false            
    view.addSubview(button)

    // if no "previous" button, hook leading constraint to its superview;
    // otherwise hook leading constraint and width to previous button

    if previousButton == nil {
        button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 5).isActive = true
    } else {
        NSLayoutConstraint.activate([
            button.leadingAnchor.constraint(equalTo: previousButton!.trailingAnchor, constant: 5),
            button.widthAnchor.constraint(equalTo: previousButton!.widthAnchor, constant: 5)
        ])
    }

    // add vertical constraints

    NSLayoutConstraint.activate([
        button.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
        view.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: 5)
    ])

    previousButton = button
}

// make sure to hook last button's trailing anchor to superview, too

view.trailingAnchor.constraint(equalTo: previousButton!.trailingAnchor, constant: 5).isActive = true
Run Code Online (Sandbox Code Playgroud)

或者,在Swift 2中:

var previousButton: UIButton?
for _ in 0 ..< buttonCount {
    let button = ...
    button.translatesAutoresizingMaskIntoConstraints = false            
    view.addSubview(button)

    // if no "previous" button, hook leading constraint to its superview;
    // otherwise hook leading constraint and width to previous button

    if previousButton == nil {
        button.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor, constant: 5).active = true
    } else {
        NSLayoutConstraint.activateConstraints([
            button.leadingAnchor.constraintEqualToAnchor(previousButton!.trailingAnchor, constant: 5),
            button.widthAnchor.constraintEqualToAnchor(previousButton!.widthAnchor, constant: 5)
        ])
    }

    // add vertical constraints

    NSLayoutConstraint.activateConstraints([
        button.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 5),
        view.bottomAnchor.constraintEqualToAnchor(button.bottomAnchor, constant: 5)
    ])

    previousButton = button
}

// make sure to hook last button's trailing anchor to superview, too

view.trailingAnchor.constraintEqualToAnchor(previousButton!.trailingAnchor, constant: 5).active = true
Run Code Online (Sandbox Code Playgroud)

并且,除了按钮之间的固定间距之外,您也可以使用UILayoutGuide(使导向器的尺寸彼此相同但是按钮宽度的百分比,无论超视图的宽度如何都能实现更自然的间距).

您也可以使用UIStackView,这是另一种在视图中均匀分布控件的好方法.

最重要的是,有很多方法可以使用autolayout实现这一目标.