【问题标题】:How to place the buttons in UIStackView next to other based on condition?如何根据条件将 UIStackView 中的按钮与其他按钮放在一起?
【发布时间】:2016-08-03 08:44:44
【问题描述】:

我有一个堆栈视图,我在其中动态添加按钮。但我需要根据以下条件添加按钮:

if trim.contains("0"){
        print("contaim 0")
        let button1 = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 20))
        button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button1.setTitle("No", forState: .Normal)
        button1.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button1.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button1)
    }
    if trim.contains("1") {
        print("contaim 1")

        let button2 = UIButton(frame: CGRect(x: 90, y: 0, width: 60, height: 20))
            button2.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button2.setTitle("Less", forState: .Normal)
            button2.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
            button2.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
            myStackview.addSubview(button2)


    }
    if trim.contains("2"){
        print("contaim 2")

        let button3 = UIButton(frame: CGRect(x: 150, y: 0, width: 60, height: 20))
        button3.setTitleColor(UIColor.blackColor(), forState: .Normal)

        button3.setTitle("Half", forState: .Normal)
        button3.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button3.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button3)
    }
    if trim.contains("3"){
        print("contaim 3")
        let button4 = UIButton(frame: CGRect(x: 210, y: 0, width: 60, height: 20))

        button4.setTitleColor(UIColor.blackColor(), forState: .Normal)

        button4.setTitle("On", forState: .Normal)
        button4.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button4.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button4)
    }
    if trim.contains("4"){
        print("contaim 4")
        let button5 = UIButton(frame: CGRect(x: 270, y: 0, width: 60, height: 20))

        button5.setTitleColor(UIColor.blackColor(), forState: .Normal)

        button5.setTitle("With", forState: .Normal)
        button5.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button5.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button5)
    }
    if trim.contains("5"){
        print("contaim 5")
        let button6 = UIButton(frame: CGRect(x: 310, y: 0, width: 60, height: 20))

        button6.setTitleColor(UIColor.blackColor(), forState: .Normal)

        button6.setTitle("On Burger", forState: .Normal)
        button6.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button6.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button6)
    }
    if trim.contains("6"){
        print("contaim 6")
        let button7 = UIButton(frame: CGRect(x: 370, y: 0, width: 60, height: 20))

        button7.setTitleColor(UIColor.blackColor(), forState: .Normal)

        button7.setTitle("On Chips", forState: .Normal)
        button7.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
        button7.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
        myStackview.addSubview(button7)
    }

这里我的按钮正在添加,但在 stackview 中的给定位置。我的要求是,例如,如果 button1 条件不满足,那么第二个按钮应该出现在第一个这样的地方??

按钮的行为应该像单选按钮

【问题讨论】:

    标签: ios swift button stack


    【解决方案1】:

    您可以使用此代码:

    var posX = 0
    var posY = 0
    if trim.contains("0"){
    print("contaim 0")
    let button1 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
    button1.setTitle("No", forState: .Normal)
    button1.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button1.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button1)
    posX = 60
    }
    if trim.contains("1") {
    print("contaim 1")
    
    let button2 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    button2.setTitleColor(UIColor.blackColor(), forState: .Normal)
    button2.setTitle("Less", forState: .Normal)
    button2.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button2.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button2)
    posX =  posX + 60
    }
    if trim.contains("2"){
    print("contaim 2")
    
    let button3 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    button3.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
    button3.setTitle("Half", forState: .Normal)
    button3.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button3.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button3)
    posX =  posX + 60
    }
    if trim.contains("3"){
    print("contaim 3")
    let button4 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    
    button4.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
    button4.setTitle("On", forState: .Normal)
    button4.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button4.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button4)
    posX =  posX + 60
    }
    if trim.contains("4"){
    print("contaim 4")
    let button5 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    
    button5.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
    button5.setTitle("With", forState: .Normal)
    button5.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button5.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button5)
    posX =  posX + 60
    }
    if trim.contains("5"){
    print("contaim 5")
    let button6 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    
    button6.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
    button6.setTitle("On Burger", forState: .Normal)
    button6.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button6.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button6)
    posX =  posX + 60
    }
    if trim.contains("6"){
    print("contaim 6")
    let button7 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
    
    button7.setTitleColor(UIColor.blackColor(), forState: .Normal)
    
    button7.setTitle("On Chips", forState: .Normal)
    button7.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
    button7.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
    myStackview.addSubview(button7)
    }
    

    【讨论】:

    • 它无法在所有条件下正常工作。例如,如果我的两个或更多 button1, button2 仅满足,则 button1 将排在第一位,但在 button2 左右。它仍然会获得位置 270,并且按钮与空格一起显示。
    • 再次检查代码。我没有运行它。它现在应该可以正常运行了
    • 还有一件事,这些按钮如何表现得像单选按钮。因为我只知道通过情节提要。不是动态的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2019-04-08
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多