2016-07-01 41 views
1

我正努力將約束添加到位置查找器按鈕我想在swift中添加到UIMapView,到目前爲止,我的約束似乎沒有任何工作,並且按鈕卡在頂端左邊角落,這裏是我的代碼:使用Swift以編程方式向按鈕添加約束

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 
} 

任何幫助將真誠地感謝。

+0

locationButton.translatesAutosizingMaskIntoContraints = false,並且locationButton.bottomAnchor.constraint(equalTo:margins.bottomAnchor,恆:-50).isActive = TRUE,似乎已經解決了我的問題。 – user6537725

+1

如果您發現自己的問題的解決方案,請將其添加爲未來Google員工的答案:) –

回答

0

locationButton.translatesAutoresizingMaskIntoConstraints = false 
相關問題