2017-10-05 147 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. 
     */