前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS16适配指南之UICalendarView

iOS16适配指南之UICalendarView

作者头像
YungFan
发布2022-08-23 15:31:32
1.2K0
发布2022-08-23 15:31:32
举报
文章被收录于专栏:学海无涯学海无涯

介绍

  • UICalendarView 是 iOS 16 中新增的视图,用于显示日历并支持同时选择日历中的一个或多个日期。
  • 只能显示年月日,无法显示时分秒,如果需要时分秒建议继续使用 UIDatePicker。

案例

代码语言:javascript
复制
//  Created by YungFan
import UIKit

class ViewController: UIViewController {
    // 创建UICalendarView
    lazy var calendarView: UICalendarView = {
        let calendarView = UICalendarView(frame: UIScreen.main.bounds)
        calendarView.backgroundColor = .white
        calendarView.tintColor = .orange
        calendarView.calendar = Calendar(identifier: .chinese)
        calendarView.locale = Locale(identifier: "zh_Hans_CN")
        calendarView.fontDesign = .rounded
        calendarView.delegate = self
        // 日期多选
        let multiDateSelection = UICalendarSelectionMultiDate(delegate: self)
        calendarView.selectionBehavior = multiDateSelection
        return calendarView
    }()

    // 用户选择的日期
    var selectedDates: Set<DateComponents> = [] {
        didSet {
            // 格式化日期
            let formatDate = selectedDates.compactMap { components in
                Calendar.current
                    .date(from: components)?
                    .formatted(.dateTime.year().month().day()
                        .locale(Locale(identifier: "zh_Hans_CN")))
            }
            .formatted()

            print(formatDate)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(calendarView)
    }
}

// MARK: - UICalendarSelectionMultiDateDelegate
extension ViewController: UICalendarSelectionMultiDateDelegate {
    // MARK: 选择了日期
    func multiDateSelection(_ selection: UICalendarSelectionMultiDate, didSelectDate dateComponents: DateComponents) {
        selectedDates.insert(dateComponents)
    }

    // MARK: 取消选择了日期
    func multiDateSelection(_ selection: UICalendarSelectionMultiDate, didDeselectDate dateComponents: DateComponents) {
        selectedDates.remove(dateComponents)
    }

    // MARK: 是否能够选择日期
    func multiDateSelection(_ selection: UICalendarSelectionMultiDate,
                            canSelectDate dateComponents: DateComponents) -> Bool {
        guard let day = dateComponents.day else {
            return false
        }
        // 只能选择偶数日
        return day.isMultiple(of: 2)
    }

    // MARK: 是否能够取消选择日期
    func multiDateSelection(_ selection: UICalendarSelectionMultiDate,
                            canDeselectDate dateComponents: DateComponents) -> Bool {
        return true
    }
}

// MARK: - UICalendarViewDelegate
extension ViewController: UICalendarViewDelegate {
    // MARK: 装饰视图,显示在日历数字下边区域
    func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents)
        -> UICalendarView.Decoration? {
        guard let day = dateComponents.day else {
            return nil
        }
        // 偶数日使用默认装饰
        if !day.isMultiple(of: 2) {
            return UICalendarView.Decoration.default(color: .systemGreen, size: .large)
        }

        return nil
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档