버튼 커스터마이징
//
// ViewController.swift
// customButton
//
// Created by MacBookPro on 2017. 12. 27..
// Copyright © 2017년 MacBookPro. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 버튼 인스턴스를 생성하고, 속성을 설정한다.
let btn = UIButton(type: UIButtonType.system) //1
btn.frame = CGRect(x:50 , y:100, width:150 , height: 30)
btn.setTitle("할수있다.", for: UIControlState.normal)
//버튼을 수평정렬
btn.center = CGPoint(x: self.view.frame.size.width / 2 , y: 100)
//루트 뷰에 버튼을 추가한다.
self.view.addSubview(btn)
//버튼의 이벤트와 메소드 btnOnclick(_:)을 연결한다.
btn.addTarget(self, action: #selector(btnOnClick(_:)), for: .touchUpInside)
}
@objc func btnOnClick(_ sender: Any){
//호출한 객체가 버튼이라면
if let btn = sender as? UIButton{
btn.setTitle("클릭되었습니다.", for: UIControlState.normal)
}
}
}