gpt4 book ai didi

ios - 如何在 SwiftUI 中实现自定义日历?

转载 作者:行者123 更新时间:2023-12-02 19:42:52 28 4
gpt4 key购买 nike

我需要使用自定义日历而不是 SwiftUI DatePicker。为此,我在创建的 MyCalendar 结构之后的 Podfile 中添加了 pod 'KDCalendar', '~> 1.8.9',如下所示:

import SwiftUI
import KDCalendar

public struct MyCalendar: UIViewRepresentable {

public func makeUIView(context: Context) -> CalendarView {
return CalendarView()
}

public func updateUIView(_ calendar: CalendarView, context: Context) {
let date: Date = Date()
calendar.selectDate(date)
}
}

当我要在任何堆栈中使用它时,它仅显示周名称,而日历的其他部分从 View 中省略,并发生以下错误:

enter image description here

[Assert] negative or zero item sizes are not supported in the flow layout

非常期望您的合作来解决此问题。

最佳答案

您也可以尝试FSCalendar。它有点复杂,有很多可配置选项。请找到FSCalendar的最简单实现如下:

用法


import UIKit
import SwiftUI
import FSCalendar

MyCalendar().frame(minWidth: 200, minHeight: 320).padding(.leading, 5)

我的日历


struct MyCalendar: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MyCalendar>) -> MyCalendarController {
let calendar: MyCalendarController = .init()
return calendar
}

func updateUIViewController(_ calendar: MyCalendarController, context: UIViewControllerRepresentableContext<MyCalendar>) {
// MARK: - TODO
}
}

MyCalendarController


class MyCalendarController: UIViewController, FSCalendarDelegateAppearance {
let secondary: UIColor = .parse(0xE0B355)
let primary : UIColor = .parse(0x346C7C)
let tersiary : UIColor = .parse(0xE7EEEF)

fileprivate let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()

fileprivate weak var calendar: FSCalendar!

override func loadView() {
let width: CGFloat = UIScreen.main.bounds.width - 40
let frame: CGRect = .init(x: 0, y: 0, width: width, height: 300)
let view: UIView = .init(frame: frame)
self.view = view

let calendar: FSCalendar = .init(frame: frame)
calendar.allowsMultipleSelection = false
calendar.dataSource = self
calendar.delegate = self

view.addSubview(calendar)
self.calendar = calendar

calendar.calendarHeaderView.backgroundColor = self.primary
calendar.calendarWeekdayView.backgroundColor = self.primary
calendar.appearance.headerTitleColor = self.tersiary
calendar.appearance.weekdayTextColor = self.tersiary

calendar.appearance.eventSelectionColor = self.tersiary
calendar.appearance.eventDefaultColor = self.primary
calendar.appearance.eventOffset = CGPoint(x: 0, y: -7)

calendar.appearance.todaySelectionColor = self.primary
calendar.appearance.selectionColor = self.secondary
calendar.appearance.todayColor = self.primary

calendar.appearance.titleWeekendColor = self.secondary
calendar.appearance.titleDefaultColor = self.primary

calendar.swipeToChooseGesture.isEnabled = true
let scopeGesture = UIPanGestureRecognizer(target: calendar, action: #selector(calendar.handleScopeGesture(_:)));
scopeGesture.delegate = self
calendar.addGestureRecognizer(scopeGesture)
}

override func viewDidLoad() {
super.viewDidLoad()
self.calendar.scope = .month
self.calendar.select(Date.init())
self.calendar.accessibilityIdentifier = "calendar"
}
}

扩展


extension MyCalendarController: FSCalendarDataSource {
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
debugPrint("did select date \(self.formatter.string(from: date))")
let selectedDates = calendar.selectedDates.map({self.formatter.string(from: $0)})
debugPrint("selected dates is \(selectedDates)")
if monthPosition == .next || monthPosition == .previous {
calendar.setCurrentPage(date, animated: true)
}
}
}

extension MyCalendarController: FSCalendarDelegate {
func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
self.calendar.frame.size.height = bounds.height
self.view.layoutIfNeeded()
}
}

extension MyCalendarController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
debugPrint("UIGestureRecognizer")
return true
}
}

extension UIColor {
static func parse(_ hex: UInt32, alpha: Double = 1.0) -> UIColor {
let red = CGFloat((hex & 0xFF0000) >> 16)/256.0
let green = CGFloat((hex & 0xFF00) >> 8)/256.0
let blue = CGFloat(hex & 0xFF)/256.0
return UIColor(red: red, green: green, blue: blue, alpha: CGFloat(alpha))
}
}

关于ios - 如何在 SwiftUI 中实现自定义日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59834349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com