移动两个按钮

时间:2016-11-05 01:57:54

标签: uibutton swift3

我有2个按钮,然后我隐藏了其中一个,我想将另一个按钮移动到此按钮的位置。

我想问一下带有约束的变体,没有它。

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

设置如下代码的按钮。

func setupButton() {
    button = UIButton(type: UIButtonType.system)
    //      SIZE
    button.bounds = CGRect(x: 0, y: 0, width: view.bounds.width / 2, height: 50)
    //      POSITION
    button.center = CGPoint(x: view.bounds.width / 2, y: 200)
    //      BACKGOUND COLOUR
    button.backgroundColor = UIColor.lightGray
    //      CORNER RADIUS
    button.layer.cornerRadius = 5
    //      TEXT COLOUR
    button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
    //      SET TEXT
    button.setTitle("press me", for: UIControlState.normal)
    //      SET FONT SIZE
    button.titleLabel!.font = UIFont.systemFont(ofSize: 17)
    //      BUTTON FUNCTION
    button.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), for: UIControlEvents.touchUpInside)
    //      ADD BUTTON TO VIEW
    view.addSubview(button)
}

然后你可以在buttonPressed功能中隐藏它,并在buttonPressed中设置你的新按钮。

相关问题