Map In View 地圖提示框


插入MapKit

import MapKit

宣告資料

var mv = MKMapView()
var btn = UIButton()

let WIDTH = UIScreen.main.bounds.size.width

viewDidLoad

view.backgroundColor = .lightGray

    btn.frame.size = CGSize(width: 100,
                            height: 60)
    btn.center = CGPoint(x: WIDTH / 2,
                         y: 75)
    btn.setTitle("地圖",
                 for: .normal)
    btn.setTitleColor(.black,
                      for: .normal)
    btn.backgroundColor = .white
    btn.layer.cornerRadius = 10
    btn.addTarget(self,
                  action: #selector(btnpressed),
                  for: .touchDown)
view.addSubview(btn)

按鈕點選動作

func btnpressed(sender: UIButton) {
    let alert = UIAlertController(title: "地圖檢視\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
                                  message: "",
                                  preferredStyle: .actionSheet)
        mv = MKMapView(frame: CGRect(x: 0,
                                     y: 44,
                                     width: alert.view.frame.width - 20,
                                     height: 257))
        alert.view.addSubview(mv)
        let cancel = UIAlertAction(title: "確定",
                                   style: .cancel,
                                   handler: nil)
        alert.addAction(cancel)
    present(alert,
            animated: true,
            completion: nil)
}

下載完整範例 OneDrive

results matching ""

    No results matching ""