【问题标题】:How to resize two UI-buttons that are side by side如何调整并排的两个 UI 按钮的大小
【发布时间】:2018-03-16 21:38:00
【问题描述】:

我有两个并排的 UI 按钮。 我需要根据以下条件调整两个 UIbuttons 的大小:

两个 UIbuttons 的布局:

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

1) 在开始阶段,UI 按钮 (1) 具有约束:右 8 点和左 :12 点,以便我可以在 UI 按钮 (1) 左侧放置另一个 UI 按钮 2,如上图所示.

问题:

我在这种情况下使用计时器。时间到了。
如何以编程方式使两个 UIbutton 的宽度相等且它们之间的间隙很小,这样 UIButton(1) 的宽度会减小而 UIButton(2) 的宽度会增加

这些 UIbutton 在开始阶段会有约束。

我可以为 Ui_button(2) 使用 isHidden,但是有没有更好的方法不显示它?

非常感谢您的帮助。

谢谢

【问题讨论】:

  • 对不起。是的,我的意思是 UIButton。
  • 您是否以编程方式添加按钮? @MilkBottle

标签: ios swift uibutton autolayout


【解决方案1】:
 /*
         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.
         */

【讨论】:

    【解决方案2】:

    我认为你可以使用EqualWidths 约束。

    情节提要中的约束

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

    输出

    【讨论】:

    • 抱歉耽搁了。如何添加约束和什么是等宽?如果你能带我走过这些步骤,我将不胜感激。这个解决方案很棒。
    • 无法跟踪您的样本。这就是我所做的。 1) 根据上图在底部添加 2 个 btns 2) 在 btn1 Left:0, bottom:20, w:320, h:38, btn2 left:8, Right:0, bottom:20, w: 上设置约束: 15,小时:38。 3) 选择 btn1 和 btn2 并设置等宽。这是行不通的。可以展示一下你的步数吗?
    • @MilkBottle:我指定的所有约束都在附加图像中可见。首先拖放两个按钮以查看并首先解决它们的左侧、底部和右侧约束。对于 button1,left 为 20,用于解析 x 轴位置。对于 y 轴,我已将其与视图中心对齐,但您可以从底部将其设置为 20。宽度很关键,不要对其进行硬编码,也不要为其创建约束。给button2 8个点的尾随约束。
    • @MilkBottle:对于button2,它有点相似,右边应该是12点,底部应该是20点,左边已经与button1对齐了12点。现在,你会得到错误,但不要担心我们将通过提供相等的宽度约束来解决它们。选择 Button1,然后按命令并选择按钮 2。现在,当两个按钮都被选中时,转到底部的 Add new Constraints 按钮并分配相等的宽度。它们将被分配相同的宽度,这是您希望它们在应用启动几秒钟后所处的状态。
    • @MilkBottle:按照这里的代码,我在 viewDidLoad 上更改了这个约束的常量。
    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多