CustomPickerButton์ ๋ง๋ค์์ผ๋ DatePicker๋ก ์งํํด๋ณด์
์๋ค์ํผ ๋๋
์ด๊ฑธ ๋ง๋ค๊ธฐ๋ก ํ๋ค
๐ UI setting
๋ณ๊ฑฐ ์๋ค.. ๊ทธ๋ฅ ในใ UI setting code
open class CustomDatePicker: UIView{
private var Titlelabel = UILabel()
private var years = [String]()
private var months = [String]()
private var days = [String]()
private let yearWidth : CGFloat = 126
private let monthWidth : CGFloat = 103
private let dayWidth : CGFloat = 103
private lazy var yearPicker = CustomPickerButton(placeholder: "YYYY", width: yearWidth, data: years)
private lazy var monthPicker = CustomPickerButton(placeholder: "MM", width: monthWidth, data: months)
private lazy var dayPicker = CustomPickerButton(placeholder: "DD", width: dayWidth, data: days)
func initAttribute(title: String){
Titlelabel = {
let label = UILabel()
label.textColor = .black
label.font = .systemFont(ofSize: 17)
label.text = title
return label
}()
}
func initAutolayout(){
[Titlelabel, yearPicker, monthPicker, dayPicker].forEach {
self.addSubview($0)
}
self.addSubview(yearPicker)
Titlelabel.snp.makeConstraints {
$0.left.equalToSuperview()
$0.top.equalToSuperview()
}
yearPicker.snp.makeConstraints {
$0.left.equalToSuperview()
$0.top.equalTo(Titlelabel.snp.bottom).offset(4)
$0.bottom.equalToSuperview()
}
monthPicker.snp.makeConstraints {
$0.left.equalTo(yearPicker.snp.right).offset(8)
$0.top.equalTo(yearPicker)
$0.bottom.equalToSuperview()
}
dayPicker.snp.makeConstraints {
$0.left.equalTo(monthPicker.snp.right).offset(8)
$0.top.equalTo(yearPicker)
$0.bottom.equalToSuperview()
$0.right.equalToSuperview()
}
}
}
์ฃผ์ํด์ผ ํ ์ ์ autolayout !!!
view์ ํฌ๊ธฐ๋ ์์น๊ฐ ์ ํํ์ง ์์ผ๋ฉด UIButton ๋์ ์ํ๊ธฐ ๋๋ฌธ์ ์์น๋ ํฌ๊ธฐ ์ค์ ์ ํด์ฃผ์๊ธธ,,
๐ date setting
๋๋ DatePicker๋ฅผ ๋ง๋ค๊ฑฐ๋๊น ๋ , ์, ์ผ์ ํธ์ถํด์ผ ํ๋ค.
๊ทผ๋ฐ ์ผ๋จ.. ์์ง 2ํธ์ด๋๊น ์ผ์ ์ ํํ ์ค์ ์ ์๋ตํ๊ณ .. ๋ , ์๋ง ์ค์ ํด๋ณด๊ฒ ์ ใ ใ
func dataSetting(){
// 1970 ~ ํ์ฌ ๋
๋ ๊น์ง
for y in 1970...dateFormatter("yyyy") {
years.append(String(y))
}
// default : 1 ~ 12
for m in 1...12{
months.append(String(m))
}
// not yet..
for d in 1...31{
days.append(String(d))
}
}
func dateFormatter(_ format : String) -> Int {
let formatter_year = DateFormatter()
formatter_year.dateFormat = format
let string_data = formatter_year.string(from: Date())
return Int(string_data)!
}
๋ ๋์ ๋์ dateFormatter๋ฅผ ์ด์ฉํด์ ํ์ฌ ๋ ๋๋ฅผ ๊ฐ์ง๊ณ ์ค๊ฒ ํด์คฌ์ !!
์ฌ์ค ์๋กท๊ฒ ํ๋ฉด CustomDatePicker ๋ณด์ฌ์ฃผ๊ธฐ์ ๊ตฌํ์ ๋์..
์ด์ ์ผ์ ์กฐ์ํด์ผ ํ๋๋ฐ ์๋ง๋ Observer๋ฅผ ์จ์ผ ํ ๊ฒ ๊ฐ์!!
๊ทธ๋์ ๋ง์ง๋ง 3์์ ์์ฃผ ์์ธํ ๋ค๋ค๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค..๐
'๐ฑ iOS > UIKit' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[UIKit] TableView Multiple Cell, multi type json data parsing (0) | 2023.06.27 |
---|---|
[UIKit] CustomDatePicker ๋ง๋ค๊ธฐ (3/3) (0) | 2023.04.03 |
[UIKit] toast message ๋ง๋ค๊ธฐ (0) | 2023.02.16 |
[UIKit] CustomDatePicker ๋ง๋ค๊ธฐ (1/3) (0) | 2023.01.08 |
Passing Data (์ง์ ์ ๋ฌ๋ฐฉ์) (0) | 2022.06.27 |