popeyelau / wiki

📒Wiki for many useful notes, source, commands and snippets.
2 stars 0 forks source link

Handle Auto Layout with different screen sizes #22

Open popeyelau opened 5 years ago

popeyelau commented 5 years ago
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