在swift中动态添加按钮

问题描述 投票:0回答:3

我正在编写一个 iOS 应用程序。服务器为我提供了按钮数组以在视图上绘制。因此,按钮的数量可以根据服务器响应而变化, 示例:

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

我必须垂直堆叠这些按钮。 我创建了一个 stackview,向其中添加 constraints,然后将 add 按钮添加到此 stackview 作为 arrangedsubviews。 按钮之间应有 5 点的间隙:

使用堆栈视图:

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)

        }

    }

没有堆栈视图:

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()
    }

我的问题是,除了上面的代码,如何应用 NSLayoutConstraintsLayoutAnchors 来解决这个问题?

ios objective-c swift autolayout constraints
3个回答
1
投票

您可以使用包含按钮的表格视图单元格来代替堆栈视图,并且行数可以是按钮数组计数。在

cellForRowAtIndexPath
委托方法中,从按钮数组中设置按钮标题。


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()
}

0
投票
override func viewDidLoad() {
    super.viewDidLoad()

    let button = NSButton()
    button.title = "My Button"
    self.view.addSubview(button)
    button.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
}
© www.soinside.com 2019 - 2024. All rights reserved.