[英]UiButton and translatesAutoresizingMaskIntoConstraints
My question is a simple one. 我的问题很简单。
I'm currently subclassing UIButton
to create some custom animations. 我目前正在子类化
UIButton
来创建一些自定义动画。 However, when I set the translatesAutoresizingMaskIntoConstraints
to false
, my view moves to the upper left corner. 但是,当我将
translatesAutoresizingMaskIntoConstraints
设置为false
,我的视图将移至左上角。 However when I set it to false OR change the class to UIView
or UIControl
this problem doesn't appear anymore. 但是,当我将其设置为false或将类更改为
UIView
或UIControl
此问题不再出现。
Here is my code: 这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = TestA(frame: CGRectMake(100, 100, 100, 100))
button.backgroundColor = UIColor.blackColor()
self.view.addSubview(button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class TestA : UIControl {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
}
Does anyone know what's going on? 有人知道发生了什么吗?
You have position A
for example and position B
. 例如,您拥有职位
A
和职位B
For position A you have added constraints and its fixed on its place. 对于位置A,您添加了约束并将其固定在其位置上。 If you just type for example
button.frame = CGRectMake(0.0.100.100)
it will not move from current position because it is fixed, in this case if we want to achieve movement we need to use translatesAutoresizingMaskIntoConstraints
and we should set it to true, after that we have possibility to change position of UIButton
. 如果仅输入例如
button.frame = CGRectMake(0.0.100.100)
,因为它是固定的,它将不会从当前位置移动,在这种情况下,如果要实现移动,我们需要使用translatesAutoresizingMaskIntoConstraints
,并将其设置为true,之后,我们就有可能更改UIButton
位置。 Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.