简体   繁体   English

UiButton并将AutoresizingMaskIntoConstraints转换

[英]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或将类更改为UIViewUIControl此问题不再出现。

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.

相关问题 translationsAutoresizingMaskIntoConstraints防止UIButton操作迅速发生 - translatesAutoresizingMaskIntoConstraints prevents UIButton actions swift 将AutoresizingMaskIntoConstraints和UILabel转换为 - translatesAutoresizingMaskIntoConstraints and UILabel translatesAutoresizingMaskIntoConstraints和IB - translatesAutoresizingMaskIntoConstraints and IB 将autoresizingMaskIntoConstraints转换为不起作用 - translatesAutoresizingMaskIntoConstraints doesn't work 无法设置translatesAutoresizingMaskIntoConstraints - Can't set translatesAutoresizingMaskIntoConstraints 从xib加载并将自动调整大小的蒙版转换为约束 - loading from xib and translatesautoresizingmaskintoconstraints 在UIPageViewController上将TranslatesAutoresizingMaskIntoConstraints设置为No - Setting TranslatesAutoresizingMaskIntoConstraints to No on UIPageViewController 设置将autoresizingMaskIntoConstraints转换为所有子视图 - Setting translatesAutoresizingMaskIntoConstraints to all subviews 如果 translatesAutoresizingMaskIntoConstraints = false,则滚动视图不起作用 - scrollview not working if translatesAutoresizingMaskIntoConstraints = false 当translatesAutoresizingMaskIntoConstraints设置为NO时,视图消失。 - Views disappear when `translatesAutoresizingMaskIntoConstraints` set `NO`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM