0

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

这是一个快速 gif 来显示到底发生了什么

动图

在这一点上,我已经尝试了一切。使用calendar.setScope()而不是calendar.scope =,约束attachedToCalendarView.topAnchorcalendar.bottomAnchor calendar.contentView.bottomAnchor,和calendar.daysContainer.bottomAnchor,甚至attachedToCalendarView根据是周范围还是范围月来打开和关闭约束。

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

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))
}

}

4

1 回答 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
    }
}
于 2018-12-10T13:09:19.667 回答