使用Swift以编程方式向按钮添加约束

时间:2016-07-01 10:32:48

标签: ios swift mkmapview

我正在努力为我想在swift中添加到UIMapView的位置查找器按钮添加约束,到目前为止,我的约束似乎都没有工作,按钮卡在左上角,这是我的代码:

override func loadView() 
{
mapView = MKMapView()
view = mapView

let margins = view.layoutMarginsGuide

let locationButton = UIButton()
mapView.addSubview(locationButton)

locationButton.setTitle("find location", for: [])
locationButton.backgroundColor = UIColor.lightGray().withAlphaComponent(0.6)
locationButton.layer.cornerRadius = 3
locationButton.sizeToFit()
locationButton.translatesAutoresizingMaskIntoConstraints = true

locationButton.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: 20).isActive = true
locationButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
locationButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
}

任何帮助都将深表感谢。

1 个答案:

答案 0 :(得分:0)

    locationButton.translatesAutoresizingMaskIntoConstraints = false