屏幕适配思路之一(swift)

思路:在AppDelegate里面获取到屏幕尺寸 计算出屏幕尺寸与某一固定型号的比例 然后对CGRect做扩充方法

```

//

//  AppDelegate.swift

//  适配demo

//

//  Created by OVIX on 2017/3/27.

//  Copyright © 2017年 OVIX. All rights reserved.

//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

//MARK: - 属性

var autoSizeScaleX: Float = 0

var autoSizeScaleY: Float = 0

var window: UIWindow?

//MARK: - 生命周期

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let screenWidth = UIScreen.main.bounds.size.width

let screenHeight = UIScreen.main.bounds.size.height

autoSizeScaleX = Float(screenWidth) / 320.0

autoSizeScaleY = Float(screenHeight) / 568.0

return true

}

```


```

//

//  Rect-extension.swift

//  适配demo

//

//  Created by OVIX on 2017/3/27.

//  Copyright © 2017年 OVIX. All rights reserved.

//

import UIKit

extension CGRect {

///全部缩放

init (ov_x: Float, ov_y: Float, ov_width: Float, ov_height: Float) {

self.init()

let appdelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate

origin.x = CGFloat(appdelegate.autoSizeScaleX * ov_x)

origin.y = CGFloat(ov_y * appdelegate.autoSizeScaleY)

size.width = CGFloat(ov_width * appdelegate.autoSizeScaleX)

size.height = CGFloat(ov_height * appdelegate.autoSizeScaleY)

}

///高度不缩放

init (ov_x: Float, ov_y: Float, ov_width: Float, height: Float) {

self.init()

let appdelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate

origin.x = CGFloat(appdelegate.autoSizeScaleX * ov_x)

origin.y = CGFloat(ov_y * appdelegate.autoSizeScaleY)

size.width = CGFloat(ov_width * appdelegate.autoSizeScaleX)

size.height = CGFloat(height)

}

}

```

你可能感兴趣的:(屏幕适配思路之一(swift))