alert 커스텀 - 이미지
alert 커스텀 - 이미지
//
// 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)
//3. 정의된 버튼을 알림창 객체에 추가한다.
alert.addAction(okAction)
//컨텐트 뷰 영역에 들어갈 컨트롤러를 생성하고, 알림창에 등록한다.
let contentVC = ImageViewController()
// 뷰 컨트롤러 알림창의 콘텐츠 뷰 컨트롤러 속성에 등록한다.
alert.setValue(contentVC, forKeyPath: "contentViewController")
//4. 알림창을 화면에 표시한다.
self.present(alert, animated: false)
}
}
//
// MapKitViewController.swift
// AlertCustom1
//
// Created by MacBookPro on 2018. 1. 3..
// Copyright © 2018년 MacBookPro. All rights reserved.
//
import UIKit
class ImageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//1.이미지와 이미지 뷰 객체 생성
let icon = UIImage(named: "rating5") //실제 이미지를 저장하는 객체
let iconV = UIImageView(image:icon) // 이미지 객체를 이용해서 화면에 표시하는 뷰의 일종
//2. 이미지 뷰의 영역과 위치를 지정
iconV.frame = CGRect(x:0, y:0, width:(icon?.size.width)!,height:(icon?.size.height)!)
//3. 루트뷰에 이미지 뷰를 추가
self.view.addSubview(iconV)
//preferredContentSize 속성을 통해 외부 객체가 ImageViewController를 나타낼 때 참고할 사이즈 지정
//4. 외부에서 참조할 뷰 컨트롤러 사이즈를 이미지 크기와 동일하게 설정 + 10은 알림창에 이미지가 표시될때 아래 여백 주기
self.preferredContentSize = CGSize(width:(icon?.size.width)!,height:(icon?.size.height)!+10)
}
}