Created
March 28, 2018 04:37
-
-
Save CenoX/b1e554bc837eae5871cd01d54119ff1e to your computer and use it in GitHub Desktop.
UIAlertController에 스피너 박기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UILoadingAlertController: UIAlertController { | |
var activityIndicator: UIActivityIndicatorView! | |
let activityRect: CGRect = CGRect(x: 116, y: 60, width: 40, height: 40) | |
var isShowing: Bool = false | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
activityIndicator = UIActivityIndicatorView(frame: activityRect) | |
activityIndicator.color = .lightGray | |
self.view.addSubview(activityIndicator) | |
activityIndicator.startAnimating() | |
} | |
/// 제목을 변경합니다. | |
func setTitle(message: String) { | |
DispatchQueue.main.async { | |
self.title = message | |
} | |
} | |
/// 메세지를 변경합니다. | |
func setMessage(message: String) { | |
DispatchQueue.main.async { | |
self.message = message | |
} | |
} | |
/// 컨트롤러를 화면에 띄웁니다. 애플리케이션의 윈도우 루트컨트롤러에 실행하게끔 코드를 작성하여 따로 뷰를 넘겨줄 필요가 없습니다. | |
/// UIViewController 등이 아닌 곳에서도 실행할 수 있는 코드입니다. | |
func show(completion: (()->())?) { | |
isShowing = true | |
DispatchQueue.main.async { | |
UIApplication.shared.keyWindow?.rootViewController?.present(self, animated: true, completion: completion ?? nil) | |
} | |
} | |
/// 컨트롤러를 화면에 띄웁니다. 필요한 경우 컨트롤러를 인자로 넘겨 컨트롤러에서 바로 실행할 수 있습니다. | |
/// UIViewController 에서는 이쪽 코드로 실행하는 것이 권장사항입니다. | |
func show(to controller: UIViewController, completion: (()->())?) { | |
isShowing = true | |
DispatchQueue.main.async { | |
controller.present(self, animated: true, completion: completion ?? nil) | |
} | |
} | |
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { | |
isShowing = false | |
super.dismiss(animated: flag, completion: completion) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment