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

alert 커스텀 - 기본

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

alert 커스텀




//

//  ViewController.swift

//  AlertCustom1

//

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

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

//


import UIKit


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: nil,preferredStyle: .alert)

        

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

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

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

        

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

        alert.addAction(okAction)

        alert.addAction(cancel)

        

        //UIViewController() 를 상속받은 어떠한 객체를 이용해서라도 커스터마이징 할 수 있다.

        //추가된 코드 - 알림창에 들어갈 뷰 컨트롤러

        let v = UIViewController()

        let lb = UILabel()

        lb.frame = CGRect(x:50, y:50 , width: 60, height: 20)

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

        lb.text = "안녕하소"

        lb.textColor = UIColor.black

        v.view.addSubview(lb)

        v.view.layer.borderColor = UIColor.blue.cgColor

        

        

        

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

        

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

        self.present(alert, animated: false)

        

        

        

        

        //기본 구문!

        //alert 객체에 contentView를 넣을 수 있다.!!

        //let v = UIViewController()

        //alert.setValue(v, forKey: "contentViewController")

        

    }



}










반응형

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

alert 커스텀 - 지도 2  (0) 2018.01.03
alert 커스텀 - 지도1  (0) 2018.01.03
메모장 - 커스텀(x)  (0) 2018.01.03
커스텀 내비게이션 바  (0) 2018.01.03
탭바 커스텀하기  (0) 2018.01.02

댓글