Formatting for both iPhone and iPad – SwiftUI

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.

New contributor

theadrianm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật