Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UiButton和Translates自动调整大小GMaskintoConstraints_Ios_Swift_Uibutton_Autolayout - Fatal编程技术网

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。