Ios UiButton和Translates自动调整大小GMaskintoConstraints
我的问题很简单。Ios UiButton和Translates自动调整大小GMaskintoConstraints,ios,swift,uibutton,autolayout,Ios,Swift,Uibutton,Autolayout,我的问题很简单。 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)
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)
}
}
我目前正在子类化
UIButton
,以创建一些自定义动画。但是,当我将translatesAutoResizengMaskintoConstraints
设置为false
时,我的视图移动到左上角。但是,当我将其设置为false或将类更改为UIView
或UIControl
时,此问题不再出现
这是我的代码:
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)
}
}
有人知道发生了什么吗?
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)
}
}
例如,您有位置
A
,位置B
。对于位置A,您添加了约束并将其固定在其位置上。如果您只是键入例如button.frame=CGRectMake(0.0.100.100)
它将不会从当前位置移动,因为它是固定的,在这种情况下,如果我们想要实现移动,我们需要使用translatesAutoResizengMackintoConstraints
并将其设置为true,之后,我们可以更改ui按钮的位置。希望有帮助。首先,我很抱歉,我应该添加代码以供审阅。无论是ui按钮
、ui视图
还是UIControl
,帧在所有情况下都硬编码为button.frame=CGRect(100100)
。但是,此行为仅当它是一个ui按钮时才会出现。translatesAutoResizengMaskinToConstraints此操作适用于ui按钮、ui视图等,但如果您具有硬编码位置,并且尝试解锁约束,则它将跳到x:0、y:0。