我正在做一个列出不同项目位置的项目,每个项目位置都与不同的时区相关联。我使用FSCalendar在时区显示当前选定项目的日期。我无法在FSCalendar视图上正确显示日期。
无论设备中配置的时区如何,我都希望在FSCalendar视图上显示日期。这意味着即使用户手动更改设备中的时区设置,此更改也不应影响我的项目日历。(通常在所有设备中都会选择自动时区)
要实现这一点,我必须使用选定的项目时区配置FSCalendar。
我尝试使用DateFormatter()将Date()转换为Project。尝试过DST的计算。
func isDateInProjectTimeZone() -> Bool {
if let projectTimeZone = AppUtils.timeZoneForProject() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
var calendar = Calendar.current
calendar.timeZone = projectTimeZone
return calendar.isDateInToday(Date().dateInProjectTimeZone())
}
return false
}发布于 2019-11-21 09:40:21
我能够通过使用FSCalendar实现以下步骤的需求,如果任何人遇到相同的场景,答案可能是有用的。
若要更改FSCalendar时区,请执行以下步骤:
导入FSCalendar类XYZFSCalendar: FSCalendar {重写init(框架: CGRect) {super.init(框架:框架)}需要init?(编码器aDecoder: NSCoder) {super.init(编码器: aDecoder),如果让projectTimeZone = { self.setValue(projectTimeZone,} self.perform(Selector(("invalidateDateTools"))) }}forKey}
,则将童话板中的上述自定义日历指定为FSCalendar
例:
import FSCalendar
class ViewController: UIVIewController {
@IBOutlet weak var calendar: XYZFSCalendar!
override func viewDidLoad() {
super.viewDidLoad()
//When you set the date below, the date will be displayed in the FSCalendar based on your custom timezone.
calendar.select = Date()
}
} 这帮助了我的方案,希望它也能帮助到需要帮助的人。timeZone可能不会在全局上发生变化,但每次使用预设的timeZone for FSCalendar实例化类时,它都会工作。
谢谢。
发布于 2021-09-15 15:23:23
我刚刚遇到了同样的问题,并通过更新项目本身来解决它。
通过从FSCalendar.h中删除属性并将其添加到,
@property (copy, nonatomic) NSTimeZone *timeZone;- (void)setTimeZone:(NSTimeZone *)timeZone
{
if (![_timeZone isEqual:timeZone]) {
_timeZone = timeZone.copy;
[self invalidateDateTools];
}
}https://stackoverflow.com/questions/58523548
复制相似问题