如何在SwiftUI中获取iPhone的屏幕宽度? [英] How to get the iPhone's screen width in SwiftUI?

查看:110
本文介绍了如何在SwiftUI中获取iPhone的屏幕宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像框的大小调整为一个正方形,该正方形与iPhone屏幕的宽度相同,因此高度的值(屏幕宽度)也相同.

I want to resize an Image frame to be a square that takes the same width of the iPhone's screen and consequently the same value (screen width) for height.

以下代码不起作用,因为它为图像提供了与视图相同的高度.

The following code don't work cause it gives the image the same height of the view.

var body: some View {
        Image("someImage")
            .resizable()
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
            .clipped()
    }

推荐答案

您可以为此创建一个 UIScreen 扩展名.喜欢:

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
}

用法:

UIScreen.screenWidth

这篇关于如何在SwiftUI中获取iPhone的屏幕宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆