|
Hi everyone! I am trying to create a SwiftUI view where I can list all the events for that day. I currently have the view working where it shows the information as long as none of the events overlap. I'm struggling to find out / figure out how to make the events split horizontally when the hour or minutes overlap for an event. This is where I am at now, some of the code is just helper data or functions:
This results in something like this: However, as you can slightly see, "Event 1" and "Event 2" are over lapped. What I would like is for them to be side-by-side so it would look like this: And of course, if more events overlapped, then it would split into 1/3, 1/4, 1/5, etc. Hopefully someone can help or provide guidance! |
|
@markb would like a date with a view specialist!
Thanks for posting your code! Your approach gives us an opportunity to review some of SwiftUI's guiding principles namely: Data Models and Declarative Views! Your Code
I like to think of the Let's review your code. In the view factory snip, I interpret it like this: Build a single But SwiftUI is also a declarative language. That is you need to declare what you want to view! New ApproachInstead, what you probably want to declare is: Layout all the This will require you to change your data model a bit. But that's the beauty of In your Next you'll want
Finally, you'll modify your Now you know how many events start at the same time, and your code within
Keep coding!These are broad concepts and guidance, you'll need to adopt to your needs. Please return here and let us all know how you solved this! Related linksHere are a few articles I wrote about view factories. They may be fun reads! See -> View Factory |
|
More about your Data ModelPlease consider your data model and the calendar view you are trying to build as two separate parts of your application. Think of two smart people in TWO different offices. Office One only sees "data". This office has ALL the events for all the days. It also knows which day the user has selected. It probably knows other information about your application's data. Office One, the Data ModelerWhen your user taps on a new date, or selects a date from a pick list, you might think about first notifying your Data Model. Send a note to Then the person in this office will get really busy. He'll create other lists (computed vars!) that will extract all the events for a given day. He'll also extract a list of start times for each activity. He's an organizing fool. Give him the tasks that organize your data. Then let him pass this to the View Painter. That is, he'll
Office Two, the View PainterThe second office is where you'll find the view painter. When the Data Modeler updates the The View Painter should not need to know the rules of Events, or the internal workings of start and end times. If she needs to add two events to the same hour, let her know the details.
Make her job easy! Just tell her what data she needs for the given hour, and she'll use the rules laid out in the view to lay out one, or several activies. |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.