UIKit
-
Hex to ColorUIKit 2020. 8. 18. 20:24
extension UIColor { convenience init(hexString: String, alpha: CGFloat = 1.0) { let hexString: String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) let scanner = Scanner(string: hexString) if (hexString.hasPrefix("#")) { scanner.scanLocation = 1 } var color: UInt32 = 0 scanner.scanHexInt32(&color) let mask = 0x000000FF let r = Int(color >> 16) & mask let g = Int(color >..
-
Notification ExampleUIKit 2020. 8. 18. 01:04
navigation으로 새로운 화면을 띄울 때, full screen으로 띄우는 것이 아니라, 아래에서 위로 올라오는 방식으로 화면을 띄운다고 해보겠습니다. 이 때, save 버튼을 누르면 새로운 view에 작성한 내용이 이전 화면의 테이블 뷰에도 나타나게 하고 싶습니다. 만약 새로운 view를 띄우는 방식이 full screen이라면, viewWillAppear에서 tableView.reloadData()를 하면 되지만, 그렇지 않다면, 위의 방법이 통하지 않습니다. 이런 경우에는, Notification 개념을 이용하여서, save 버튼을 눌렀을 때, 테이블이 reload 되도록 합니다. 우선 변경됐다라고 신호를 보낼 View에서 extenstion으로 notification 이름부터 추가해줍니다. ..
-
Alert ControllerUIKit 2020. 8. 18. 00:33
alert의 모양은 총 두가지로, Alert View와 Action Sheet 가 있습니다. -> Action Sheet는 보통 여러 가지 선택지가 놓여져 있는 경우, 사용합니다. extension UIViewController{ func alert(title: String = "알림", message: String){ let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let okAction = UIAlertAction(title: "확인", style: .default, handler: nil) alert.addAction(okAction) present(alert, animated: true, co..
-
Date Formatter (원하는 모습으로 날짜 출력)UIKit 2020. 8. 17. 22:11
let formatter: DateFormatter = { let f = DateFormatter() f.dateStyle = .long f.timeStyle = .short f.locale = Locale(identifier: "Ko_kr") return f }() 클로저를 사용하여, formatter 변수를 만듦과 동시에 설정까지 해줍니다. 그리고나서 formatter.string(from: target.insertDate)를 추가하여 from의 변수를 위에서 설정한 모양대로 나오도록 해줍니다. 아래는 결과이미지입니다.
-
Notification CenterUIKit 2020. 7. 4. 18:32
Notification Center는 객체들이 서로 상호작용할 수 있도록 하는 방법입니다. 다시 말하면, 특정 객체가 Notification Center에 등록된 event를 발생시키면 해당 event를 처리할 것이라고 등록된 observer들이 event에 대한 행동을 취하는 것이 Notification 이 동작하는 방식입니다. keyboard를 예로 들자면 1. 먼저 event를 발생시키는 객체에서 event를 Notification Center에 등록합니다. 2. event를 실행할 객체에서 observer로 등록합니다. override func viewWillAppear(_ animated: Bool) { NotificationCenter.default.addObserver(self, select..
-
View Controller의 생명주기UIKit 2020. 7. 4. 17:15
view controller는 위의 이미지와 같은 생명주기를 가집니다. 하나씩 살펴보자면 1. viewDidLoad 뷰의 컨트롤러가 메모리에 올라가고 나서 실행되는 함수입니다. 시스템에 의해 자동으로 호출이 되기 때문에 리소스를 초기화하거나 화면을 처음 켜졌을 때의 상황을 보여주는 역할을 합니다. 화면이 처음 띄워질 때 한번만 실행됩니다. 2. viewWillAppear viewDidLoad 와 같이 뷰가 나타나기 전에 실행되는 함수이지만 viewDidLoad는 한번만 실행되는 반면 viewWillAppear은 여러번 실행될 수 있습니다. (다른 view에 갔다가 다시 돌아왔을 경우에도 해주고 싶은 처리가 있다면 viewWillAppear을 사용합니다.) 3. viewDidAppear viewDidApp..