2017-10-05 37 views
0

我有兩個並排的UI按鈕。 我需要調整兩個UIbuttons底座上以下條件:如何調整並排的兩個UI按鈕

2個UIbuttons的佈局:

 (1)    (2) 
|-------------------||-| 
| UIButton   ||B| 
|-------------------||-| 

1)在開始階段中,UI-按鈕(1)與約束:右8點左邊:12點,這樣我可以在UI按鈕(1)的左邊放置另一個UI按鈕2,如上所示。

問題:

我使用這種情況下的計時器。當時間到了。
如何編程使兩個的UIButton具有相等的寬度和它們之間的小間隙,使得UIButton的(1)的寬度將減少和的UIButton(2)的寬度將增加

這些的UIButton將具有約束在開始階段。

我可以使用isHidden爲Ui_button(2),但有沒有更好的方式不顯示它?

非常感謝您的幫助。

感謝

+0

遺憾。是的,我的意思是UIButton。 – MilkBottle

+0

你是否以編程方式添加按鈕? @MilkBottle – jegadeesh

回答

0

我認爲你可以EqualWidths約束玩。

約束在故事板

enter image description here

enter image description here

的ViewController代碼

class ViewController: UIViewController { 
    @IBOutlet weak var button2WidthContstraint:NSLayoutConstraint! // outlet for equal widths constraint for button2. 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     button2WidthContstraint.constant = 50 - self.view.bounds.size.width; 
     setUpTimer() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func setUpTimer() { 
     Timer.scheduledTimer(withTimeInterval: 4, repeats: false) {[weak self] (timer) in 
      self?.setButtonWidthEqual() 
      timer.invalidate() 
     } 
    } 

    func setButtonWidthEqual() { 
     button2WidthContstraint.constant = 0 
     UIView.animate(withDuration: 0.2) { 
      self.view.layoutIfNeeded() 
     } 
    } 

} 

輸出

enter image description here

+0

這很酷!我很快就會回到這裏。 – MilkBottle

+0

對延遲抱歉。我如何添加約束和什麼是等寬?如果你能通過步驟走路,我會很感激。這個解決方案非常棒。 – MilkBottle

+0

無法遵循年份樣本。這就是我所做的。 1)在底部添加2 btns按照上述圖像2)在btn1上設置constriants左:0,底部:20,w:320,h:38,btn2左:8,右:0,底部:20,w: 15,小時:38。 3)選擇btn1和btn2並設置等寬。這不起作用。你能顯示你的步驟嗎? – MilkBottle

0
/* 
     Add Button 1 from storyboard and add constraints: (left:0, top: 0) 
     Add Button 2 from storyboard and add constraints: (left:10, right 0, width: 44) (Initial width) 
     Now made out let of Button_2's width 

     The place where u need to make the width of both button same write the following code. 
     */ 

     self.button2Width.constant = (self.view.frame.size.width-10)/2 
     UIView.animate(withDuration: 0.2) { 
      self.view.layoutIfNeeded() 
     } 

     /* 
     we reduce 10 from total view width because there is only 10 point space in both buttons & there is no leading trailing space. 
     means zero leading trailing space. 
     -> If you specify any leading trailing space then minus that space from total width. 
     */