How do I scale the height of FSCalendar?

4.3k views Asked by At

I am using an FSCalendar in my project and I am wondering how to make my calendar scale to a new height that is being animated? I have an "eventsView" that is being swiped up and down and when I change the height of my calendar, the white border line that marks the edge of the calendar changes but the dates aren't scaling to take up the entire space given whether that be bigger or smaller than after the view loads. Any help would be great, I am working in swift 3, and the fscalendar framework installed via cocoa pods.

2

There are 2 answers

0
Alexandre G On

Ok it's ridiculous and wrong, but this is the only way I could find how to set height in the half an hour after I should've been home, without changing the library.

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    super.didRotate(from: fromInterfaceOrientation)
    setupCalendarHeight()
}

func setupCalendarHeight() {
   let height = calendar.height-calendar.preferredHeaderHeight- calendar.preferredWeekdayHeight
    calendar.contentView.height = height
    calendar.daysContainer.height = height
    calendar.collectionView.height = height
    calendar.collectionViewLayout.invalidateLayout()
 }

If someone knows a better way please do let us know.

1
lukszar On

Solution

There is solution to accomplish resizing height of FSCalendar view.

(Tested on version 2.8.4)

In UIViewController which contains FSCalendar view do following steps:

  1. Add variable for FSCalendar view's height constraint:

    private var calendarHeight: NSLayoutConstraint?
    
  2. Configure initial constraint f.ex. in viewDidLoad():

     calendarHeight = calendar.heightAnchor.constraint(equalToConstant: 280)
     calendarHeight?.isActive = true
    

Important: value which I used as height 280.0 has influence on its subviews layout. Presented value is working best for me. I assume that it's connected with default height for calendar view and based on that all other subviews are laid out.

  1. Add FSCalendarDelegate function implementation like below:

     func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
         calendarHeight?.constant = bounds.height
         view.layoutIfNeeded()
     }
    
  2. Remember to add self as delegate provider, f.ex. in viewDidLoad():

    calendar.delegate = self
    

To sum up, we create constraint which is holding calendar view height based on bounding rect calculated by FSCalendar and returning after each change.