我正在努力为要快速添加到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
}

任何帮助将由衷的感谢。

最佳答案


    locationButton.translatesAutoresizingMaskIntoConstraints = false

关于ios - 使用Swift以编程方式向按钮添加约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38143164/

10-12 06:54