Search code examples
xcodeswiftuiuiscreen

UIScreen.main.bounds.width Is deprecated


in SwiftUI,my var has UIScreen.main.bounds.width.

@State private var buttonWidth: Double = UIScreen.main.bounds.width.

UIScreen.main.bounds.width Is deprecated. (main is deprecated) how can i solve?

thank you


Solution

  • Add the .frame() modifier to your button in your view and let it occupy the screen width infinitely

    var body: some View {
        YourButtonClass()
        .frame(maxWidth: .infinity)
    }   
    

    Here's documentation on the frame modifier