0

I'm using FSCalendar in my app. I placed the searchbar, which works properly with tableview below the FSCalendar. I select any row in the table and go to the next viewcontroller to edit selected row. As soon as I returned to the main VC (with FSCalndar), my searchbar disappeared. I have the same searchbar with tableview in other VC (without FSCalendar), and it works properly every time. My code for FSCalendar, tableview and searchbar are below. Does anybody have any suggestions? Probably somebody faced with similar issue before? Thanks in advance!

class CalendarViewController: UIViewController {

var calendarHeightConstraint: NSLayoutConstraint!

private var calendar: FSCalendar = {
    let calendar = FSCalendar()
    calendar.translatesAutoresizingMaskIntoConstraints = false
    return calendar
}()
let tableView: UITableView = {
    let tableView = UITableView()
    tableView.translatesAutoresizingMaskIntoConstraints = false
    return tableView
}()
let searchController = UISearchController(searchResultsController: nil)
var currentSearch = ""

override func viewDidLoad() {
    super.viewDidLoad()
    calendar.delegate = self
    calendar.dataSource = self
    calendar.scope = .week
    calendar.firstWeekday = 2
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.register(CalendarTableViewCell.self, forCellReuseIdentifier: idCalendarCell)
    configureSearchBar()
    setConstraints()        
}

 private func configureSearchBar() {
    searchController.searchBar.delegate = self
    searchController.delegate = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "search for events"
    navigationItem.searchController = searchController
    definesPresentationContext = true
}

extension CalendarViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredEvents.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: idCalendarCell, for: indexPath) as! CalendarTableViewCell
    .................. (code)
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 75
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    goToEditEvent(filteredEvents[indexPath.row])
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
       .......... (code)
       }
   }
}

extension CalendarViewController: FSCalendarDataSource, FSCalendarDelegate {

func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
    calendarHeightConstraint.constant = bounds.height
    view.layoutIfNeeded()
}

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    selectedDate = date
    refreshEvent()
}

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    for i in 0..<allEvents.count {
        if date.onlyDate == allEvents[i].eventDate.onlyDate {
            return 1
        }
    }
    return 0
}
}

extension CalendarViewController {

func setConstraints() {
    view.addSubview(calendar)
    calendarHeightConstraint = NSLayoutConstraint(item: calendar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 300)
    calendar.addConstraint(calendarHeightConstraint)
    
    NSLayoutConstraint.activate([
        calendar.topAnchor.constraint(equalTo: view.topAnchor, constant: 130),
        calendar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        calendar.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
    ])
    
    view.addSubview(showHideButton)
    NSLayoutConstraint.activate([
        showHideButton.topAnchor.constraint(equalTo: calendar.bottomAnchor, constant: 0),
        showHideButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
        showHideButton.widthAnchor.constraint(equalToConstant: 200),
        showHideButton.heightAnchor.constraint(equalToConstant: 20)
    ])
    
    view.addSubview(tableView)
    NSLayoutConstraint.activate([
        tableView.topAnchor.constraint(equalTo: showHideButton.bottomAnchor, constant: 10),
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
    ])
}
}

'''

OleksandrS
  • 11
  • 2
  • Hi, everybody! I've found the decision myself. Because a FSCalendar is scrollable, it was necessary to set: "navigationItem.hidesSearchBarWhenScrolling = false", to avoid the hiding of the searchBar from the View. Probably it could be helpful for somebody in the future. – OleksandrS Nov 05 '22 at 14:36

0 Answers0