gpt4 book ai didi

ios - UiButton 并将 AutoresizingMask 翻译成约束

转载 作者:行者123 更新时间:2023-11-30 13:58:49 24 4
gpt4 key购买 nike

我的问题很简单。

我目前正在对 UIButton 进行子类化以创建一些自定义动画。但是,当我将 translatesAutoresizingMaskIntoConstraints 设置为 false 时,我的 View 会移至左上角。但是,当我将其设置为 false 或将类更改为 UIViewUIControl 时,此问题不再出现。

这是我的代码:

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


}

有人知道发生了什么事吗?

enter image description here

enter image description here

最佳答案

例如,您拥有位置 A 和位置 B。对于位置 A,您添加了约束并将其固定在其位置上。例如,如果您只是输入 button.frame = CGRectMake(0.0.100.100) 它不会从当前位置移动,因为它是固定的,在这种情况下,如果我们想实现移动,我们需要使用 translatesAutoresizingMaskIntoConstraints 我们应该将其设置为 true,之后我们就有可能更改 UIButton 的位置。希望对您有所帮助。

关于ios - UiButton 并将 AutoresizingMask 翻译成约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33246353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com