Swift 3 - 限制按钮宽度

Swift 3 - Constrain Button Width

我想在我的视图顶部创建一个简单的栏,并排放置两个按钮,每个按钮占 50%。

我是这样创建栏的:

    let topTabView = UIView()
    topTabView.backgroundColor = UIColor.red
    topTabView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(topTabView)

    topTabView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    topTabView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
    topTabView.heightAnchor.constraint(equalToConstant: 60).isActive = true
    topTabView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

这有效。

然后我添加了我的两个按钮,我得到了所有的约束,除了我不确定如何让宽度锚点工作,所以每个按钮占据 50%查看.

    let filterButton = UIButton()
    filterButton.backgroundColor = UIColor.red
    filterButton.setTitle("Filter", for: .normal)
    filterButton.translatesAutoresizingMaskIntoConstraints = false
    topTabView.addSubview(filterButton)

    filterButton.leftAnchor.constraint(equalTo: topTabView.leftAnchor).isActive = true
    filterButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
    filterButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true

    // NOT SURE ABOUT THIS ONE
    filterButton.widthAnchor.constraint(equalToConstant: 200).isActive = true


    let mapButton = UIButton()
    mapButton.backgroundColor = UIColor.red
    mapButton.setTitle("Map", for: .normal)
    mapButton.translatesAutoresizingMaskIntoConstraints = false
    topTabView.addSubview(mapButton)

    mapButton.rightAnchor.constraint(equalTo: topTabView.rightAnchor).isActive = true
    mapButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
    mapButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true

    // NOT SURE ABOUT THIS ONE
    mapButton.widthAnchor.constraint(equalToConstant: 200).isActive = true

我试过类似的方法,但没有用:

mapButton.widthAnchor.constraint(equalToConstant: toTabView.frame.width / 2.0).isActive = true

任何帮助都会很棒!

so that each button takes up 50% of the view

这就是 multiplier 的用途。你没有使用它。使用它!

因此,您需要一个约束,其中按钮的宽度与父视图的宽度相同,但其 multiplier0.5 值除外。

示例:

    let b1 = UIButton()
    b1.translatesAutoresizingMaskIntoConstraints = false
    b1.backgroundColor = .green
    b1.setTitle("Button1", for: .normal)
    b1.setTitleColor(.black, for: .normal)

    let b2 = UIButton()
    b2.translatesAutoresizingMaskIntoConstraints = false
    b2.backgroundColor = .yellow
    b2.setTitle("Button2", for: .normal)
    b2.setTitleColor(.black, for: .normal)

    self.view.addSubview(b1)
    self.view.addSubview(b2)

    NSLayoutConstraint.activate([
        b1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
        b2.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),

        b1.leadingAnchor.constraint(equalTo:self.view.leadingAnchor),
        b2.leadingAnchor.constraint(equalTo:b1.trailingAnchor),

        b1.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
        b2.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),

    ])

结果: