简体   繁体   English

Swift:如何移动在情节提要中添加的按钮位置

[英]Swift: How to move button position that added in Storyboard

I have added one button into one UIViewController in storyboard. 我已将一个按钮添加到情节提要中的一个UIViewController中。 Now I just want to try how can I move the origin x and y of that button from code so I write this code below 现在我只想尝试如何从代码中移动该按钮的原点x和y,所以我在下面编写此代码

    @IBOutlet weak var testBtn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()


    testBtn.frame = CGRectMake(0, 0, 200, 10)

}

but The button not move. 但是按钮不动。 Do you know why? 你知道为什么吗?

You need to place it in viewDidAppear. 您需要将其放置在viewDidAppear中。 Even viewWillLoad if not late enough.. Here is a good way to set it with little hassle: 即使不是很晚也可以使用viewWillLoad。这是设置它的一种好方法,几乎​​没有麻烦:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Put the coordinates you want instead of x: 0, y: 0
    testBtn.center = CGPointMake(0, 0)
}

Remember that if you override this method, you must call super at some point in your implementation as recommended in the apple documentation. 请记住,如果您重写此方法,则必须按照Apple文档中的建议在实现的某个时刻调用super。 While not obligatory, it should relieve many problems ;) 虽然不是强制性的,但它可以缓解许多问题;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM