2016-08-03 111 views
0

我有一個stackview,我在其中動態添加按鈕。但我需要添加基於如下條件的按鈕:如何根據條件將按鈕放在UIStackView旁邊?

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

這裏我的按鈕添加,但在堆棧視圖中的給定位置。我的要求是,例如,如果button1條件不滿足,那麼第二個按鈕應該在這樣的第一個地方?

按鈕應該表現得像單選按鈕

回答

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) 
} 
+0

它不是在所有條件下正常工作。比如說,如果我的兩個或兩個以上的button1,button2只滿足,那麼button1將會排在第一位,但是會看到button2。它仍然會獲得位置270並且按鈕正在顯示空間。 –

+0

只有在丟失1個按鈕的情況下,您的代碼纔有效 –

+0

請再次檢查代碼。我沒有運行它。它現在應該正常運行 – ADK