Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect positioning of calendar with AutoLayout #389

Open
Serproger opened this issue Nov 1, 2024 · 1 comment
Open

Incorrect positioning of calendar with AutoLayout #389

Serproger opened this issue Nov 1, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Serproger
Copy link

The modern way to build app interfaces is Auto Layout based on building constraints connecting views. Let's suppose we have some simple screen with one label at the top and calendar that occupies the remaining part of screen. KVKCalendarView has here the red background.

import KVKCalendar
import EventKit
import Foundation

class ViewController: UIViewController {

	var events = [Event]()
	
	private var label = {
		let label = UILabel()
		label.numberOfLines = 0
		label.translatesAutoresizingMaskIntoConstraints = false
		label.text = "Label with a very very very very very very very very very very very very very very very very very very long text"
		return label
	}()
	
	private lazy var calendarView: KVKCalendarView = {
		var style = Style()
		style.defaultType = .week
		let calendar = KVKCalendarView(frame: CGRect.zero, style: style)
		calendar.dataSource = self
		calendar.backgroundColor = .red
		return calendar
	}()
	
	private var calendarContainerView = {
		let view = UIView()
		view.translatesAutoresizingMaskIntoConstraints = false
		return view
	}()
	
	override func viewDidLoad() {
		super.viewDidLoad()
		view.addSubview(label)
		view.addSubview(calendarContainerView)
		debugPrint(view.safeAreaInsets)
		label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
		label.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
		label.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
		calendarContainerView.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
		calendarContainerView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
		calendarContainerView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
		calendarContainerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
		calendarContainerView.addSubview(calendarView)
		createEvents { (events) in
			self.events = events
			self.calendarView.reloadData()
		}
	}
		
	override func viewDidLayoutSubviews() {
		super.viewDidLayoutSubviews()
		calendarView.reloadFrame(calendarContainerView.frame)
	}
		
	func createEvents(completion: ([Event]) -> Void) {
		let formatter = DateFormatter()
		formatter.dateFormat = "dd.MM.yyyy HH:mm"
		
		var firstEvent = Event(ID: "1")
		firstEvent.start = formatter.date(from: "31.10.2024 10:30")!
		firstEvent.end = formatter.date(from: "31.10.2024 11:00")!
		firstEvent.color = Event.Color(.red)
		
		var secondEvent = Event(ID: "2")
		secondEvent.start = formatter.date(from: "01.11.2024 14:00")!
		secondEvent.end = formatter.date(from: "01.11.2024 15:30")!
		secondEvent.color = Event.Color(.green)

		completion([
			firstEvent,
			secondEvent
		])
	}
}

extension ViewController: CalendarDataSource {
	func eventsForCalendar(systemEvents: [EKEvent]) -> [KVKCalendar.Event] {
		return events
	}
}

This screen is displayed so (obviously it's incorrect):
image

If we move reloadFrame call to viewWillLayoutSubviews as written in docs, we'll get this:
image

Similar bugs can be reproduced even without Auto layout, it's enough to pass to reloadFrame any frame with origin different from zero (yes, it can happen, when there are another views in the controller and calendar isn't at the top).

Please elaborate how can we achieve the correct positioning without big empty spaces?

@Serproger Serproger added the bug Something isn't working label Nov 1, 2024
@kvyatkovskys
Copy link
Owner

@Serproger Hello! I'll check this case and try to fix the issue. JFYI, the library doesn't work correctly with Auto Layout😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants