Skip to content

Instantly share code, notes, and snippets.

@wotjd
Created November 6, 2018 15:47
Show Gist options
  • Save wotjd/359836397959ebd40c148accfd356975 to your computer and use it in GitHub Desktop.
Save wotjd/359836397959ebd40c148accfd356975 to your computer and use it in GitHub Desktop.
StackView에 동적으로 버튼 추가
//
// StackViewController.swift
// TableViewTest
//
// Created by edge on 06/11/2018.
// Copyright © 2018 castis. All rights reserved.
//
import UIKit
// TODO : Font 옵션 추가
struct AlertButtonStyle {
var backgroundColor : UIColor
var textColor : UIColor
var text : String
// var textFont : UIFont
}
enum AlertButtonType : Int {
case Cancel
case OK
var defaultStyle : AlertButtonStyle {
switch self {
case .Cancel:
return AlertButtonStyle(
backgroundColor: UIColor.init(displayP3Red: 153/255, green: 153/255, blue: 153/255, alpha: 1),
textColor: UIColor.white, text: "취소")
default:
return AlertButtonStyle(
backgroundColor: UIColor.init(displayP3Red: 1, green: 33/255, blue: 33/255, alpha: 1),
textColor: UIColor.white, text: "확인")
}
}
}
/* FIXME : AlertButtonManager 추가
1. UIButton 리스트 관리
2. AlertButtonType 리스트 관리
3. should be addable
4. Font 적용 가능하도록
- 콜백 처리 부분 구상해야함
- StackView 에 추가하는 기능은 X (이건 ViewController 에서)
*/
class StackViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
// move to manager
var buttonTypes : [AlertButtonType] = []
var buttons : [UIButton] = []
override func viewDidLoad() {
super.viewDidLoad()
self.addButton(with: .Cancel)
self.addButton(with: .OK)
self.loadButtons()
self.addButtons(buttons: self.buttons)
}
private func addButtons(buttons: [UIButton]) {
for button in buttons {
self.stackView.addArrangedSubview(button)
}
}
// move to manager
public func addButton(with type: AlertButtonType) {
self.buttonTypes.append(type)
}
// move to manager
// TODO : Font 적용
func loadButtons() {
for type in self.buttonTypes {
let button = UIButton.init()
button.backgroundColor = type.defaultStyle.backgroundColor
button.setTitleColor(type.defaultStyle.textColor, for: .normal)
button.setTitle(type.defaultStyle.text, for: .normal)
self.buttons.append(button)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment