Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Auto Layout with different screen sizes #22

Open
popeyelau opened this issue Jul 16, 2019 · 0 comments
Open

Handle Auto Layout with different screen sizes #22

popeyelau opened this issue Jul 16, 2019 · 0 comments
Labels
Code snippets 代码片段 iDev iDev 相关 Swift Swift 相关 Tips&Tricks 奇技淫巧

Comments

@popeyelau
Copy link
Owner

class Device {
    // 以 iPhone 6 尺寸为设计稿
    static let base: CGFloat = 375

    static var ratio: CGFloat {
        return UIScreen.main.bounds.width / base
    }
}

xtension CGFloat {

    var adjusted: CGFloat {
        return self * Device.ratio
    }
}

extension Double {

    var adjusted: CGFloat {
        return CGFloat(self) * Device.ratio
    }
}

extension Int {
    var adjusted: CGFloat {
        return CGFloat(self) * Device.ratio
    }
}
label.font = UIFont.systemFont(ofSize: 23.adjusted)

phoneTextField.leftAnchor.constraint(equalTo: container.leftAnchor, constant: 30.adjusted)
phoneTextField.rightAnchor.constraint(equalTo: container.rightAnchor, constant: -30.adjusted)

imageView.widthAnchor.constraint(equalToConstant: 80.adjusted)
imageView.heightAnchor.constraint(equalToConstant: 90.adjusted)

How to handle Auto Layout with different screen sizes

@popeyelau popeyelau added Swift Swift 相关 Code snippets 代码片段 Tips&Tricks 奇技淫巧 iDev iDev 相关 labels Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code snippets 代码片段 iDev iDev 相关 Swift Swift 相关 Tips&Tricks 奇技淫巧
Projects
None yet
Development

No branches or pull requests

1 participant