꾸준히 안타치기

알림창 / 지도넣기, 이미지 넣기 본문

iOS/기본편 | 실전편 -꼼꼼한재은씨

알림창 / 지도넣기, 이미지 넣기

글자줍기 2022. 2. 2. 14:43
반응형

버튼과 알림창 내용사이의 공간에 뷰컨트롤러를 넣어서 커스텀 할 수 있다.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 기본 알림창 버튼 생성
        let defaultAlertBtn = UIButton(type: .system)
        defaultAlertBtn.frame = CGRect(x: 0, y: 100, width: 100, height: 30)
        defaultAlertBtn.center.x = self.view.frame.width / 2
        defaultAlertBtn.setTitle("기본 알림창", for: .normal)
        defaultAlertBtn.addTarget(self, action: #selector(defaultAlert(_:)), for: .touchUpInside)
        
        self.view.addSubview(defaultAlertBtn)
    }
    
    
    @objc func defaultAlert(_ sender: Any) {
        // 1) 알림창을 정의한다. (타이틀과 메시지영역을 없앰)
        let alert = UIAlertController(title: "알림창제목",
                                      message: "알림창내용",
                                      preferredStyle: .alert)
        
        // 2) 버튼을 정의한다.
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let okAction = UIAlertAction(title: "OK", style: .default)
        
        // 3) 버튼을 알림창에 추가한다.
        alert.addAction(cancelAction)
        alert.addAction(okAction)
        
        // 알림창에 들어갈 뷰 컨트롤러******************
        let v = UIViewController()
        v.view.backgroundColor = .white
        
        // 알림창에 뷰 컨트롤러를 등록한다.(setValue 사용)
        alert.setValue(v, forKey: "contentViewController")
        
        // 4) 알림창을 화면에 표시한다.
        self.present(alert, animated: false)
    }
    
}

얼럿창에 지도 한번에 작성 / 클래스 따로 작성 안함 

더보기
import UIKit
import MapKit

class MapAlertViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 버튼생성
        let alertBtn = UIButton(type: .system)
        
        // 버튼 속성 설정
        alertBtn.frame = CGRect(x: 0, y: 150, width: 100, height: 30)
        alertBtn.center.x = self.view.frame.width / 2
        alertBtn.setTitle("Map Alert", for: .normal)
        alertBtn.addTarget(self, action: #selector(mapAlert(_:)), for: .touchUpInside)
        
        self.view.addSubview(alertBtn)
    }
    
    @objc func mapAlert( _ sender: UIButton) {
        // 경고창 객체를 생성하고, OK 및 Cancel 버튼을 추가한다.
        let alert = UIAlertController(title:nil, message: "여기가 맞습니까?",
                                      preferredStyle: .alert)
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let okAction = UIAlertAction(title: "OK", style: .default)
        
        alert.addAction(cancelAction)
        alert.addAction(okAction)
        
        // 콘텐츠 뷰 영역에 들어갈 뷰 컨트롤러를 생성하고, 알림창에 등록한다.*************
        let contentVC = UIViewController()
        // 뷰 컨트롤러를 알림창의 콘텐츠 뷰 컨트롤러 속성에 등록한다.
        let mapkitView = MKMapView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        contentVC.view = mapkitView
        contentVC.preferredContentSize.height = 200
        
        // 위치정보를 설정한다. 위도경도를 사용한다.
        let pos = CLLocationCoordinate2D(latitude: 37.514322, longitude: 126.894623)
        
        // 지도에서 보여줄 넓이, 일종의 축척. 숫자가 작을수록 좁은 범위를 확대시켜서 보여준다.
        let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
        
        // 지도영역을 정의
        let region = MKCoordinateRegion(center: pos, span: span)
        
        // 지도뷰에 표시
        mapkitView.region = region
        mapkitView.regionThatFits(region)
        
        // 위치를 핀으로 표시
        let point = MKPointAnnotation()
        point.coordinate = pos
        mapkitView.addAnnotation(point)
        
        // 뷰컨트롤러 알림창의 콘텐츠 뷰 컨트롤러 속성에 등록한다.
        alert.setValue(contentVC, forKey: "contentViewController")
        
        self.present(alert, animated: false)
    }
}

 

중간에 지도 넣기 / 클래스 따로 작성

MapAlertViewController ( 알림창띄우는 뷰컨 )

import UIKit
import MapKit

class MapAlertViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 버튼생성
        let alertBtn = UIButton(type: .system)
        
        // 버튼 속성 설정
        alertBtn.frame = CGRect(x: 0, y: 150, width: 100, height: 30)
        alertBtn.center.x = self.view.frame.width / 2
        alertBtn.setTitle("Map Alert", for: .normal)
        alertBtn.addTarget(self, action: #selector(mapAlert(_:)), for: .touchUpInside)
        
        self.view.addSubview(alertBtn)
    }
    
    @objc func mapAlert( _ sender: UIButton) {
        // 경고창 객체를 생성하고, OK 및 Cancel 버튼을 추가한다.
        let alert = UIAlertController(title:nil, message: "여기가 맞습니까?",
                                      preferredStyle: .alert)
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let okAction = UIAlertAction(title: "OK", style: .default)
        
        alert.addAction(cancelAction)
        alert.addAction(okAction)
        
        // 콘텐츠 뷰 영역에 들어갈 뷰 컨트롤러를 생성하고, 알림창에 등록한다.*************
        // 클래스 따로생성
        let contentVC = MapKitViewController()
      
        // 뷰컨트롤러 알림창의 콘텐츠 뷰 컨트롤러 속성에 등록한다.
        alert.setValue(contentVC, forKey: "contentViewController")
        
        self.present(alert, animated: false)
    }
    
}

 

MapKitViewController ( 지도위치 클래스 )

import UIKit
import MapKit

class MapKitViewController: UIViewController {
    
    override func viewDidLoad() {
        
        // 뷰컨트롤러에 맵킷뷰를 추가한다.
        let mapkitView = MKMapView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        self.view = mapkitView
        self.preferredContentSize.height = 200
        
        // 위치정보를 설정한다. 위도경도를 사용한다.
        let pos = CLLocationCoordinate2D(latitude: 37.512312, longitude: 126.894623)
        
        // 지도에서 보여줄 넓이, 일종의 축척. 숫자가 작을수록 좁은 범위를 확대시켜서 보여준다.
        let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
        
        // 지도영역을 정의
        let region = MKCoordinateRegion(center: pos, span: span)
        
        // 지도뷰에 표시
        mapkitView.region = region
        mapkitView.regionThatFits(region)
        
        // 위치를 핀으로 표시
        let point = MKPointAnnotation()
        point.coordinate = pos
        mapkitView.addAnnotation(point)
    }
    
}

이미지넣기

MapAlert 클릭 -> 알림창 생성

ImageAlertViewController

import UIKit
import MapKit

class ImageAlertViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 버튼생성
        let alertBtn = UIButton(type: .system)
        
        // 버튼 속성 설정
        alertBtn.frame = CGRect(x: 0, y: 150, width: 100, height: 30)
        alertBtn.center.x = self.view.frame.width / 2
        alertBtn.setTitle("Map Alert", for: .normal)
        alertBtn.addTarget(self, action: #selector(mapAlert(_:)), for: .touchUpInside)
        
        self.view.addSubview(alertBtn)
    }
    
    @objc func mapAlert( _ sender: Any) {
        // 경고창 객체를 생성하고, OK 및 Cancel 버튼을 추가한다.
        let alert = UIAlertController(title:nil, message: "이번 글의 평점은 다음과 같습니다.",
                                      preferredStyle: .alert)
        
//        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let okAction = UIAlertAction(title: "OK", style: .default)
        
//        alert.addAction(cancelAction)
        alert.addAction(okAction)
        
        // 콘텐츠 뷰 영역에 들어갈 뷰 컨트롤러를 생성하고, 알림창에 등록한다.*************
        let contentVC = imageViewController()
        alert.setValue(contentVC, forKey: "contentViewController")
        self.present(alert, animated: false)
    }
}

 

ImageViewController

import UIKit

class imageViewController :UIViewController{
    
    override func viewDidLoad() {
        // ① 이미지와 이미지 뷰 객체를 생성
        let icon = UIImage(named: "rating5") //별다섯개이미지
        let iconV = UIImageView(image: icon )
        
        // ② 이미지 뷰의 영역과 위치를 지정
        iconV.frame = CGRect(x: 0, y: 0,
                             width:(icon?.size.width)!,
                             height:(icon?.size.height)!)
        
        // ③ 루트 뷰에 이미지 뷰를 추가
        self.view.addSubview(iconV)
        
        // ④ 외부에서 참조할 뷰 컨트롤러 사이즈를 이미지 크기와 동일하게 설정
        self.preferredContentSize = CGSize(width: (icon?.size.width)!,
                                           height: (icon?.size.height)!+10)
    }
}

 

 

https://developer.apple.com/documentation/uikit/uialertcontroller/

 

Apple Developer Documentation

 

developer.apple.com

 

반응형
Comments