我已经给了标签,我给出了X-Y轴的值,但是我的值没有把标签放在每个设备的中心,我写了宽度大小的方法,如果设备是大的设置宽度大小50更小,但是这个方法不能在iPhone SE,5S/6/6S/7中使用iPhone 6 +/6S+/7 +!!举个例子
那么有什么问题,我应该补充什么呢??
let lengthOfChar : CGFloat = data.ans.length // Characters form SQLite database
let yAxis : CGFloat = self.view.frame.height / 3 * 1.8
let width: CGFloat = view.frame.size.width - 40 // frame width
var targetWidth: CGFloat = (width - (lengthOfChar - 1) * 5) / lengthOfChar
if targetWidth > 50 {
targetWidth = 50
}
let totalWidth: CGFloat = (targetWidth * lengthOfChar) + ((lengthOfChar - 5) * 5)
let x : CGFloat = (width / 2) - (totalWidth / 2)
let xx : CGFloat = (CGFloat(indexTar) * targetWidth) + (CGFloat(indexTar) * 5) + 20
var xAxis : CGFloat = (x + xx)
xAxis = width - xAxis
最佳答案
我正在使用一个helper枚举,它允许为某些iphone执行自定义操作。设置下面的枚举和用法。
使用以下代码添加新类:
enum DeviceType: Int {
case iPhone4 = 1
case iPhone5
case iPhone6
case iPhone6Plus
case iPad
init(userInterfaceIdiom: UIUserInterfaceIdiom, screenHeight: CGFloat) {
switch (userInterfaceIdiom, screenHeight) {
case (.phone, 0..<568.0):
self = .iPhone4
case (.phone, 568.0..<667.0):
self = .iPhone5
case (.phone, 667.0..<736.0):
self = .iPhone6
case (.phone, 736.0..<CGFloat.infinity):
self = .iPhone6Plus
case (.pad, _):
self = .iPad
default:
self = .iPhone6
}
}
func isSameOrSmaller(than deviceType: DeviceType) -> Bool {
return self.rawValue <= deviceType.rawValue
}
}
用法:
if UIDevice.current.type().isSameOrSmaller(than: .iPhone5) {
// Do what you need
}
关于ios - Swift 3-为每个设备设置X-Y轴UILabel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45486236/