I can’t get my view – MyAccountView – to display correctly in both iPad and iPhone. I have to change code every time. The problem is this piece:
` ZStack {
if UIDevice.current.userInterfaceIdiom == .pad {
(colorScheme == .dark ? Color.black : Color.white)
.edgesIgnoringSafeArea(.all)
// Dynamic Animated Gradient
}`
When I have the ZStack right below the var body the it displays everything correctly for iOS, but when I try to open it on iPad OS I get a completely blank screen except for a small button in the top left corner. When I remove the ZStack then it works correctly on iPad OS but in IOS I get a completely blank screen.
Full code:
`struct MyAccountView: View {
@StateObject private var userProfileViewModel = UserProfileViewModel()
@State private var showSettingsSheet = false
@State private var showHighlightsSheet = false
@State private var showNotesSheet = false
@State private var showBookmarkedPostsSheet = false
@State private var highlightsSheet: HighlightsView?
@State private var notesSheet: NotesView?
@State private var bookmarkedPostsSheet: BookmarkedPostsView?
@State var rotation: Double = 0
@State var scale: CGFloat = 1
@Environment(.colorScheme) var colorScheme // Access the current color scheme
var body: some View {
ZStack {
if UIDevice.current.userInterfaceIdiom == .pad {
(colorScheme == .dark ? Color.black : Color.white)
.edgesIgnoringSafeArea(.all)
// Dynamic Animated Gradient
}
Circle()
.fill(LinearGradient(
gradient: Gradient(colors: colorScheme == .dark ?
[Color(red: 222/255, green: 78/255, blue: 98/255), Color(red: 161/255, green: 58/255, blue: 161/255)] :
[Color(red: 245/255, green: 200/255, blue: 220/255), Color(red: 230/255, green: 210/255, blue: 240/255)]), // Stronger shades for light mode
startPoint: .top,
endPoint: .bottom
))
.frame(width: 300, height: 300)
.blur(radius: 40)
.opacity(colorScheme == .dark ? 0.8 : 1) // Changed opacity for light mode
.rotationEffect(Angle.degrees(rotation))
.scaleEffect(scale)
.onAppear() {
animateIndefinitely()
}
if UIDevice.current.userInterfaceIdiom == .phone {
(colorScheme == .dark ? Color.black : Color.white)
.edgesIgnoringSafeArea(.all)
}
ZStack(alignment: .topTrailing) {
VStack {
UserProfileView(viewModel: userProfileViewModel)
.padding()
ShimmeringProgressBar(xp: Double(userProfileViewModel.xp), level: userProfileViewModel.level)
.padding(.horizontal)
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 150))], spacing: 20) {
NavigationLink(destination: HighlightsView(highlights: userProfileViewModel.highlights)) {
AccountButton(title: "Highlights", icon: "star.fill")
}
NavigationLink(destination: NotesListView(viewModel: userProfileViewModel)) {
AccountButton(title: "Notes", icon: "note.text")
}
NavigationLink(destination: BookmarkedPostsView(bookmarkedPosts: $userProfileViewModel.bookmarkedPosts)) {
AccountButton(title: "Bookmarks", icon: "bookmark.fill")
}
NavigationLink(destination: FriendsView(viewModel: userProfileViewModel)) {
AccountButton(title: "Friends", icon: "person.2.fill")
}
}
.padding()
}
Spacer()
}
.padding(.top, 60)
Button(action: {
showSettingsSheet = true
}) {
Image(systemName: "gearshape")
.font(.title)
.padding()
}
.sheet(isPresented: $showSettingsSheet) {
SettingsSheetView()
}
}
}
}
private func updateHighlights() {
if let highlightsView = highlightsSheet {
userProfileViewModel.updateHighlights(highlightsView.editedHighlights)
}
}
private func updateNotes() {
if let notesView = notesSheet {
userProfileViewModel.updateNotes(notesView.editedNotes)
}
}
private func updateBookmarkedPosts() {
if let bookmarkedPostsView = bookmarkedPostsSheet {
userProfileViewModel.bookmarkedPosts = bookmarkedPostsView.editedPosts
}
}
private func animateIndefinitely() {
let rotationAnim = Animation.linear(duration: 10).repeatForever(autoreverses: false)
let scaleAnim = Animation.easeInOut(duration: 2).repeatForever(autoreverses: true)
withAnimation(rotationAnim) {
rotation += 360
}
withAnimation(scaleAnim) {
scale = 1.2
}
}
}`
I’ve tried adding code that will make the ZStack only on iOS but it still doesn’t work, and I’ve also tried moving around other pieces and watching tutorials.
theadrianm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.