上記の構造体のメソッドはstaticなメソッドなので以下の様に呼び出した結果
問題なく実行したiPhoneの画面サイズを取得できました。
structがclassのスコープの中に入っているようにもとれますが、外に出してくださいね。
まだ分からなところがありました質問ください。
iPhone6Sで実行した場合
swift
1print(UIScreenUtil.bounds())
2//=> (0.0, 0.0, 375.0, 667.0)
3
4print(UIScreenUtil.screenWidth())
5//=> 375
6
7print(UIScreenUtil.screenHeight())
8//=> 667
iPhone5で実行した場合
swift
1print(UIScreenUtil.bounds())
2//=> (0.0, 0.0, 320.0, 568.0)
3
4print(UIScreenUtil.screenWidth())
5//=> 320
6
7print(UIScreenUtil.screenHeight())
8//=> 568
swift
1import UIKit
2
3class ViewController: UIViewController {
4
5 override func viewDidLoad() {
6 super.viewDidLoad()
7
8 // メソッド呼び出し
9 print(UIScreenUtil.bounds())
10 }
11}
12
13
14struct UIScreenUtil {
15 static func bounds()->CGRect{
16 return UIScreen.mainScreen().bounds;
17 }
18 static func screenWidth()->Int{
19 return Int( UIScreen.mainScreen().bounds.size.width);
20 }
21 static func screenHeight()->Int{
22 return Int(UIScreen.mainScreen().bounds.size.height);
23 }
24}