Sunday, July 14, 2024
HomeiOS Developmentios - SwiftUI format does not look the identical on all gadgets

ios – SwiftUI format does not look the identical on all gadgets


I’ve put in my take a look at code on two of my telephones. I’ve an iPhone XS Max and an iPhone 11. The app seems to be totally different on every cellphone and I’m not certain find out how to repair it so it seems to be the identical.

I’ve connected two pictures the primary one is on the iPhone XS Max and that’s how I need it to look on all gadgets. The second is on the iPhone 11.

I’ll connect my code right here.

import SwiftUI
import AVFoundation

// Mannequin
class PostItem: ObservableObject, Identifiable {
    @Revealed var merchandise: String = ""
}

// ViewModel
class ProfileViewModel: ObservableObject {
    @Revealed var offerImages: [UIImage] = [UIImage(named: "AppIcon")!, UIImage(named: "AppIcon")!, UIImage(named: "AppIcon")!]
    @Revealed var posts: [PostItem] = []
    @Revealed var occupation: String = "Career"
    @Revealed var bio: String = "Bio"
    @Revealed var bi: String = "BI"
    @Revealed var bioText: String = "Bio textual content"
    @Revealed var proText: String = "Professional textual content"
    @Revealed var biText: String = "BI textual content"
    @Revealed var currentImageIndex: Int = 0
    
    personal var participant: AVAudioPlayer!
    personal var timer: Timer?
    
    init() {
        for _ in 0..<6 {
            let newItem = PostItem()
            newItem.merchandise = "It is a take a look at submit to see the character restrict"
            posts.append(newItem)
        }
        startTimer()
    }
    
    func startTimer() {
        timer?.invalidate()
        timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { [weak self] _ in
            self?.slideToNext()
        }
    }
    
    personal func slideToNext() {
        withAnimation {
            currentImageIndex = (currentImageIndex + 1) % offerImages.rely
        }
    }
    
    personal func playSound() {
        guard let url = Bundle.principal.url(forResource: "TEST MUSIC", withExtension: "mp3") else { return }
        participant = strive? AVAudioPlayer(contentsOf: url)
        participant.numberOfLoops = -1 // Loop indefinitely
        participant.play()
    }
}

// Customized Button Type
struct PrimaryButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.95 : 1.0)
            .padding()
            .body(width: 100, peak: 50)
            .background(
                Picture("gradient 1")
                    .resizable()
                    .scaledToFill()
            )
            .foregroundColor(.white)
            .cornerRadius(8)
            .offset(x: 0, y: -100)
    }
}

// Customized Assortment View
struct CollectionView: View {
    var pictures: [UIImage]
    @Binding var currentIndex: Int
    
    var physique: some View {
        GeometryReader { geometry in
            ZStack {
                ForEach(0..<pictures.rely, id: .self) { index in
                    if index == currentIndex {
                        Picture(uiImage: pictures[index])
                            .resizable()
                            .body(width: geometry.measurement.width, peak: geometry.measurement.peak)
                            .transition(.opacity)
                    }
                }
            }
        }
    }
}



// Foremost View
struct ProfileScreenView: View {
    @StateObject personal var viewModel = ProfileViewModel()
    
    var physique: some View {
        ZStack {
            CollectionView(pictures: viewModel.offerImages, currentIndex: $viewModel.currentImageIndex)
                .edgesIgnoringSafeArea(.all)
            
            ScrollView {
                VStack {
                    Picture(uiImage: UIImage(named: "blank_profile")!)
                        .resizable()
                        .scaledToFit()
                        .body(width: 99, peak: 99)
                        .clipShape(Circle())
                        .padding()
                    
                    HStack {
                        Button("Join") {
                            // Deal with join motion
                        }
                        .buttonStyle(PrimaryButtonStyle())
                        
                        Spacer()
                        
                        Button("Faucet In") {
                            // Deal with faucet in motion
                        }
                        .buttonStyle(PrimaryButtonStyle())
                    }
                    .padding()
                    
                    Record(viewModel.posts) { submit in
                        Textual content(submit.merchandise)
                            .background(Colour.purple)
                    }
                    .background(Colour.blue)
                    .body(peak: 300)
                    
                    ProfileDetailsView()
                        .offset(x: 48, y: -390)
                }
                .padding()
            }
        }
    }
}

struct ProfileScreenView_Previews: PreviewProvider {
    static var previews: some View {
        ProfileScreenView()
    }
}

I’ve tried utilizing the show width and peak to scale the weather I additionally used GeometryReader to scale factor. I anticipated that to repair all the pieces though it simply reduce off a part of the app on the iPhone 11.

iPhone XS Max
iPhone 11



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments