iOS Swift - Calendar

하재은·2022년 4월 10일
0

iOS

목록 보기
18/19
post-thumbnail

1. 달력을 사용

코코아팟에 FSCalendar 설치하기
pod 'FSCalendar' > import FSCalendar > command key + B 누르기
첫번째와 두번째를 순서대로 입력후 빌드하기.
(빌드하지 않을 경우 제대로 실행되지 않을 수 있음)

2. 달력 추가하기

storyboard에 View를 추가 > Custom Class - Class 부분에 [FSCalendar] 입력하면 달력 생성 완료

3. 기능 및 디자인

먼저 아울렛 변수로 등록하기 > @IBOutlet var calendarView: FSCalendar!
(1) 배경색 > calendarView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
(2) 선택한 날짜 색 > calendarView.appearance.selectionColor = UIColor(red: 38/255, green: 153/255, blue: 251/255, alpha: 1)
(3) 오늘 날짜 색 > calendarView.appearance.todayColor = UIColor(red: 188/255, green: 224/255, blue: 253/255, alpha: 1)
(4) 선택 날짜 모서리 둥글게 > calendarView.appearance.borderRadius = 0
(둥근 정도를 설정하는 것으로 0은 뾰족한 모서리)

4. 달력 스크롤

스크롤 활성화 > calendarView.scrollEnabled = true
스크롤 방향 설정 > calendarView.scrollDirection = .vertical

참고.https://gonslab.tistory.com/13


5. 날짜 선택시 해당일 출력하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import UIKit
import FSCalendar
 
class MainViewController: UIViewController, FSCalendarDataSource, FSCalendarDelegate {
    
    
    @IBOutlet var calendar: FSCalendar!
    
 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        calendar.allowsMultipleSelection = true
        calendar.delegate = self
        calendar.dataSource = self
 
        // Do any additional setup after loading the view.
    }
    
    
    // 날짜 선택 시 콜백 메소드
    public func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd"
        print(dateFormatter.string(from: date))
    }
 
    // 날짜 선택 해제 시 콜백 메소드
    public func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd"
        print(dateFormatter.string(from: date))
    }
 
    
    
 
}
 
 
출처: https://hyongdoc.tistory.com/334 [Doony Garage]
cs

0개의 댓글

관련 채용 정보