본문 바로가기
ios 뽀개기/실전

alert 커스텀 - 지도 2

by 인생여희 2018. 1. 3.
반응형

alert 커스텀 - 지도 2





//

//  ViewController.swift

//  AlertCustom1

//

//  Created by MacBookPro on 2018. 1. 3..

//  Copyright © 2018년 MacBookPro. All rights reserved.

//


import UIKit

import MapKit

class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        

        //버튼 객체 만들기

        let btn = UIButton(type:.system)

        btn.frame =  CGRect(x:50, y:50 , width: 200, height: 100)

        btn.center = CGPoint(x:self.view.frame.width / 2, y: self.view.frame.height / 2)

        btn.setTitle("alert", for: UIControlState.normal)

        btn.backgroundColor = UIColor.brown

        btn.layer.borderColor = UIColor.blue.cgColor

        btn.layer.borderWidth = 4

        

        self.view.addSubview(btn)

        

        btn.addTarget(self, action: #selector(alertAction(_:)), for: .touchUpInside)

        

    }


    @objc func alertAction(_ sender:Any){

        

        //1. 알림창을 경고 형식으로 정의 한다.

        let alert = UIAlertController(title: nil, message: "여기가 맞나요?",preferredStyle: .alert)

        

        //2. 버튼을 정의 한다.

        let okAction = UIAlertAction(title: "ok", style: .default)

        let cancel = UIAlertAction(title: "cancel", style: .cancel)

        

        //3. 정의된 버튼을 알림창 객체에 추가한다.

        alert.addAction(okAction)

        alert.addAction(cancel)

        

        

        //4. 알림창을 화면에 표시한다.

        self.present(alert, animated: false)

        

        

        

        //컨텐트 뷰 영역에 들어갈 컨트롤러를 생성하고, 알림창에 등록한다.

        let contentVC = MapKitViewController()

        

        

       

        // 뷰 컨트롤러 알림창의 콘텐츠 뷰 컨트롤러 속성에 등록한다.

        alert.setValue(contentVC, forKeyPath: "contentViewController")

        

        



        

    }



}






//

//  MapKitViewController.swift

//  AlertCustom1

//

//  Created by MacBookPro on 2018. 1. 3..

//  Copyright © 2018년 MacBookPro. All rights reserved.

//


import UIKit

import  MapKit

class MapKitViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()


        //뷰 컨트롤러에 맵킷 뷰를 추가한다.

        

        let mapkitView = MKMapView(frame: CGRect(x:0, y:0 , width: 0, height: 0))

        //맵킷뷰를 루트 뷰로 정했다. 그래서 위에 위치와 높이 넓이 값을 0으로 맞췄다.

        self.view = mapkitView

        // 이 값은 외부(알림창)에서 뷰 컨트롤러를 읽어 들일 때 참고하는 사이즈

        //알림창은 contentVC 객체의 정보를 읽어 들일 때 preferredContentSize 속성을 참고하여

        //표현해야할 크기가 얼마인지 계산한다.

        //이 속성의 높이 값이 200으로 주어져 있으므로 콘텐츠 영역에도 200만큼의 높이가 할당된다.

        self.preferredContentSize.height = 200

        

        

        //1 위치 정보를 설정한다.

        let pos = CLLocationCoordinate2D(latitude:37.5593582, longitude:126.95928759999993)

        

        //2. 지도에서 보여줄 넓이 // 일종의 축적// 숫자가 작을 수록 좁은 범위를 확대 시켜 보여 준다.

        let span = MKCoordinateSpan(latitudeDelta:0.005, longitudeDelta:0.005)

        

        //3. 지도 영역을 정의

        let region = MKCoordinateRegion(center:pos, span:span)

        //4 지도 뷰에 표시

        mapkitView.region = region

        mapkitView.regionThatFits(region)

        

        //5 위치를 핀으로 표시

        let point = MKPointAnnotation()

        point.coordinate = pos

        mapkitView.addAnnotation(point)

        

        

        

        

    }



}


반응형

'ios 뽀개기 > 실전' 카테고리의 다른 글

alert 커스텀 슬라이더  (0) 2018.01.03
alert 커스텀 - 이미지  (0) 2018.01.03
alert 커스텀 - 지도1  (0) 2018.01.03
alert 커스텀 - 기본  (0) 2018.01.03
메모장 - 커스텀(x)  (0) 2018.01.03

댓글