在swift中动态添加按钮

时间:2017-06-07 09:58:46

标签: ios objective-c swift autolayout constraints

我正在写一个iOS应用程序。服务器给我按钮数组在视图上绘制。因此,按钮的数量可能会根据服务器响应而变化, 例如:

let buttonArray=["Button 1","Button 2","Button 3"]
or
let buttonArray=["Button 1","Button 2","Button 3"," Button 4", "Button 5"]

我必须垂直堆叠这些按钮。 我创建了 stackview ,添加约束,然后添加按钮作为 arrangesubviews 添加到此堆栈视图。 按钮之间应该有 5分的差距:

使用stackview:

func addButtonsUsingStackView()
    {
        //create stackview:
        let stackView=UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        stackView.spacing = 5
        stackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackView)

        //stackview constraints:
        let viewsDictionary = ["stackView":stackView]
        let stackView_H = NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-20-[stackView]-20-|",
            options: NSLayoutFormatOptions(rawValue: 0),
            metrics: nil,
            views: viewsDictionary)
        let stackView_V = NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-30-[stackView]-30-|",
            options: NSLayoutFormatOptions(rawValue:0),
            metrics: nil,
            views: viewsDictionary)
        view.addConstraints(stackView_H)
        view.addConstraints(stackView_V)

        //adding buttons to stackview:

        //let buttonArray=["Button 1","Button 2","Button 3"]
        let buttonArray=["Button 1","Button 2","Button 3"," Button 4"]

        for buttonName in buttonArray{

            let button=UIButton()
            button.setTitle(buttonName, for: .normal)
            button.setTitleColor(UIColor.white, for: .normal)
            button.backgroundColor=UIColor.blue
            button.translatesAutoresizingMaskIntoConstraints=false

            stackView.addArrangedSubview(button)

        }

    }

没有stackview:

var buttonArray=["Button 1","Button 2","Button 3"," Button 4"," Button 5"," Button 6"," Button 7"]

    func addButtonsLoop()
    {

        for _view in view.subviews{
            _view.removeFromSuperview()
        }

        var i=0
        var buttonY = 20
        let buttonGap=5

        for btn in buttonArray {

            let buttonHeight=Int(Int(view.frame.height) - 40 - (buttonArray.count * buttonGap))/buttonArray.count
            print(buttonHeight)
            let buttonWidth=Int(view.frame.width - 40)

            let button = UIButton()
            button.backgroundColor = UIColor.orange
            button.setTitle(btn, for: .normal)
            button.titleLabel?.textColor = UIColor.white
            button.frame = CGRect(x: 20, y: buttonY, width:buttonWidth , height:buttonHeight)
            button.contentMode = UIViewContentMode.scaleToFill
            buttonY += buttonHeight + buttonGap
            button.tag = i
            button.addTarget(self, action: #selector(self.buttonTapped(_:)), for: UIControlEvents.touchUpInside)
            view.addSubview(button)

            i+=1

        }
    }

    func buttonTapped( _ button : UIButton)
    {
        buttonArray.remove(at: button.tag)
        addButtonsLoop()
    }

我的问题是,如何应用 NSLayoutConstraints LayoutAnchors 来解决此问题,而不是上面的代码?

2 个答案:

答案 0 :(得分:1)

您可以使用滚动视图添加按钮。

var btnY = 5
let btnHeight = 40
func addButtonsUsingStackView()
        {
    for view in self.view.subviews{
                view.removeFromSuperview()
            }

            for i in 0..< buttonArray.count {

                let btnFloor = UIButton()
                btnFloor.backgroundColor = Orange       
                btnFloor.titleLabel?.textColor = UIColor.white
                btnFloor.frame = CGRect(x: 10, y: btnY, width: Int(scrView.frame.width - 20), height: btnHeight)
                btnFloor.contentMode = UIViewContentMode.scaleToFill
                btnY += btnHeight + 5
                btnFloor.tag = buttonArray.index(of: i)
                btnFloor.addTarget(self, action: #selector(self.btnTappedFloor(_:)), for: UIControlEvents.touchUpInside)
                self.view.addSubview(btnFloor)
            }
            return cell
        }

 func btnTappedFloor( _ button : UIButton)
 {
      buttonArray.remove(at: button.tag)
      addButtonsUsingStackView()
}

答案 1 :(得分:0)

您可以使用包含按钮的tableview单元格的tableview而不是堆栈视图,行数可以是按钮数。在cellForRowAtIndexPath委托方法中,从按钮数组中设置按钮标题。