美文网首页iOS Developer
屏幕适配思路之一(swift)

屏幕适配思路之一(swift)

作者: OVIX | 来源:发表于2017-04-13 23:36 被阅读0次

思路:在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)

    本文链接:https://www.haomeiwen.com/subject/ljabwttx.html