簡體   English   中英

使用Swift以編程方式向按鈕添加約束

[英]Add constraints to a button programmatically using Swift

我正在努力為我想要添加到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
}

任何幫助將是真誠的感謝。

    locationButton.translatesAutoresizingMaskIntoConstraints = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM