iOS的自动布局问题

相关代码:

1
2
3
4
5
6
7
8
9
let deButton = UIButton(frame: CGRect(x: 0, y: 0 , width: 160, height: 100))
deButton.setTitle("TL", forState: .Normal)
deButton.backgroundColor = UIColor.greenColor()
self.view.addSubview(deButton)

deButton.snp_makeConstraints { (make) -> Void in
make.left.equalTo(self.view).offset(20)
make.top.equalTo(self.view).offset(70)
}

疑问:这段代码执行后,deButton的长和宽根据title的内容自动调节,而不是预先定义的160x100

原因:使用autolayout实现自动布局的时候,控件的frame设置将被忽略,所以要在自动布局的时候,把长宽也一同设置。

Contents
,