首页 文章

当我将范围切换到月份时,FSCalendar内容会被压扁

提问于
浏览
0

如果有一个视图受限于其底部锚点,那么当我将其范围从一周切换到另一个月时,我的FSCalendar的内容会缩小 .

Here is a quick gif to show what exactly is happening

GIF

我已经尝试了一切 . 使用 calendar.setScope() 而不是 calendar.scope = ,将 attachedToCalendarView.topAnchor 约束为 calendar.bottomAnchor calendar.contentView.bottomAnchorcalendar.daysContainer.bottomAnchor ,甚至将 attachedToCalendarView 's constraints on and off depending on whether it'周转换为范围或范围月份 .

不知道还有什么可以尝试 . 这是代码:

import UIKit
import FSCalendar

class TestController : UIViewController, FSCalendarDataSource, FSCalendarDelegate, FSCalendarDelegateAppearance {

fileprivate weak var calendar: FSCalendar!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    setUp()
}


@objc func switchCalendarScope(){
    if self.calendar.scope == FSCalendarScope.month {
        self.calendar.scope = FSCalendarScope.week

    } else {
        self.calendar.scope = FSCalendarScope.month
    }

}

func setUp(){

    let calendar = FSCalendar()
    calendar.dataSource = self
    calendar.delegate = self
    self.calendar = calendar

    self.calendar.scope = .week
    self.calendar.locale = Locale(identifier: "en_EN")
    self.calendar.calendarHeaderView.calendar.locale =  Locale(identifier: "en_EN")
    self.calendar.adjustsBoundingRectWhenChangingMonths = true

    let testingView = UIView()
    testingView.backgroundColor = .red

    let attachedToCalendarView = UIView()
    attachedToCalendarView.backgroundColor = .blue

    view.addSubview(calendar)
    view.addSubview(testingView)
    view.addSubview(attachedToCalendarView)

    self.calendar.translatesAutoresizingMaskIntoConstraints = false
    self.calendar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    self.calendar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    self.calendar.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
    self.calendar.heightAnchor.constraint(equalToConstant: 300).isActive = true

    testingView.translatesAutoresizingMaskIntoConstraints = false
    testingView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    testingView.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
    testingView.heightAnchor.constraint(equalToConstant: 20).isActive = true

    attachedToCalendarView.translatesAutoresizingMaskIntoConstraints = false
    attachedToCalendarView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    // Attaching this view's topAnchor to the calendar's bottom anchor
    attachedToCalendarView.topAnchor.constraint(equalTo: self.calendar.contentView.bottomAnchor).isActive = true
    attachedToCalendarView.widthAnchor.constraint(equalToConstant: view.bounds.size.width).isActive = true
    attachedToCalendarView.heightAnchor.constraint(equalToConstant: 20).isActive = true


    // Title and button to toggle the calendar scope
    self.navigationItem.title = "Test"
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Toggle", style: .done, target: self, action: #selector(switchCalendarScope))
}

}

1 回答

  • 0

    好吧,我无法弄清楚如何解决问题本身,但我确实找到了解决方法 . 我将日历放在一个空的容器视图(只是一个简单的UIView)中,并将 attachedToCalendarView 附加到容器的 bottomAnchor 而不是日历本身 .

    但请注意,使用 setScope (动画转换)仍然会导致相同的问题 . 要使它工作,你必须像 calendar.scope = x 手动设置它

    例:

    @objc func switchCalendarScope(){
        if self.calendar.scope == FSCalendarScope.month {
            // self.calendar.setScope(FSCalendarScope.week, animated: true) // this will cause the calendar to be squished again
            self.calendar.scope = .week
            movingConstraint.constant = view.safeAreaLayoutGuide.layoutFrame.size.height * -0.20
        } else {
            // self.calendar.setScope(FSCalendarScope.month, animated: true)
            self.calendar.scope = .month
            movingConstraint.constant = 0
        }
    }
    

相关问题