UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hiding chartYAxis if phone is in portait mode

Forums > Swift

Hi. I have a chart and I only want to display the chartYAxis if the phone is in portrait mode. If in landscape mode i'd like to display the chartYAxis. Any idea how I can do this?

2      

@flaneur's design doesn't quite fit their requirements:

I only want to display the chartYAxis if the phone is in portrait mode.
If in landscape mode i'd like to display the chartYAxis.

It seems to me that you want one view that fits in a horizontal orientation, and another view that fits in a vertical orientation. Hmmmm, if only SwiftUI offered a view that fits option!

struct DemonstrateViewThatFits: View {
    var body: some View {
        // Let SwiftUI pick!
        // Which one fits best when iPhone is horizontal?
        ViewThatFits(in: .horizontal) {
            HorizontalDesign(backgroundColor: .teal)
            VerticalDesign()
        }
    }
}

struct HorizontalDesign: View {
    var backgroundColor: Color = .indigo
    var body: some View {
        ZStack {
            backgroundColor.ignoresSafeArea()
            HStack(spacing: 10) {
                Image(systemName: "arrowshape.left").rotationEffect(.degrees(90))
                Text("Lift Controls").fontWeight(.heavy)
                Image(systemName: "arrowshape.left").rotationEffect(.degrees(-90))
            }
            .foregroundColor(.white)
            .frame(width: 700)  // <- Large number to make this NOT fit in a vertical mode
        }
    }
}

struct VerticalDesign: View {
    var backgroundColor = Color.yellow
    var body: some View {
        ZStack {
            backgroundColor.ignoresSafeArea()
            VStack(spacing: 10) {
                Image(systemName: "arrowshape.left").rotationEffect(.degrees(90))
                Text("Lift Controls").fontWeight(.heavy)
                Image(systemName: "arrowshape.left").rotationEffect(.degrees(-90))
            }
        }
    }
}

There are many more options. This is an over simplified example.
Google SwiftUI and ViewThatFits for more articles and videos.

Keep Coding!

Please come back and share your working code example!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.