How to get the iPhone's screen width in SwiftUI? How to get the iPhone's screen width in SwiftUI? ios ios

How to get the iPhone's screen width in SwiftUI?


You can create a UIScreen extension for the same. Like:

extension UIScreen{   static let screenWidth = UIScreen.main.bounds.size.width   static let screenHeight = UIScreen.main.bounds.size.height   static let screenSize = UIScreen.main.bounds.size}

Usage:

UIScreen.screenWidth


Try using Geometry Reader

let placeholder = UIImage(systemName: "photo")! // SF Symbolsstruct ContentView: View {    var body: some View {        GeometryReader { geometry in            Image(uiImage: placeholder)             .resizable()            .frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)            // .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)            .clipped()        }    }}

enter image description here


You can use UIScreen.main.bounds .width or .height

.frame(   width:UIScreen.main.bounds.width,   height:UIScreen.main.bounds.height)


matomo