如何在iOS上使用代码获取屏幕尺寸?

50

如何获取iPhone屏幕尺寸进行计算?

12个回答

0

0
var PT: CGFloat = 0;

override func viewDidLoad() {

    super.viewDidLoad()
    setPoints();

}

func setPoints() {

    let screenBounds = UIScreen.main.bounds

    let screenScale = UIScreen.main.scale

    let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)

    
    let nativeBounds = UIScreen.main.nativeBounds

    let nativeScale = UIScreen.main.nativeScale

    
    let pxScreenWidth:CGFloat = nativeBounds.width;

    let ptScreenWidth:CGFloat = screenBounds.width;

    PT = pxScreenWidth/ptScreenWidth/nativeScale;

}

使用方法:

let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接