Ask Your Question
3

How to make a calendar layout that is horizontal using Swiftui?

asked 2021-08-07 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-03 01:00:00 +0000

woof gravatar image

Here's an example of a horizontal calendar layout using SwiftUI:

struct CalendarView: View {

    private let weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    private let dates = (1..<32).map { $0 }

    var body: some View {
        VStack {
            HStack {
                ForEach(weekDays, id: \.self) { day in
                    Text(day)
                        .font(.headline)
                        .padding(.horizontal, 4)
                        .frame(width: UIScreen.main.bounds.width / 7)
                }
            }
            Divider()
            VStack {
                ForEach(dates, id: \.self) { date in
                    Text(String(date))
                        .padding(.horizontal, 4)
                        .frame(width: UIScreen.main.bounds.width / 7)
                }
            }
        }
    }
}

In this example, we have an array of week days and an array of dates. We use two ForEach loops to display them as a calendar, with the week days displayed horizontally and the dates displayed vertically. We use UIScreen.main.bounds.width to dynamically calculate the width of each cell based on the screen size.

Note that this is just a simple example and you may want to customize the layout and styling to better fit your needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-07 11:00:00 +0000

Seen: 13 times

Last updated: Apr 03 '23