在iPhone X上运行时,视图上的对象与在其他设备(如iPhone 6)上运行时的位置不同。
例如,在这张照片中,当我在iPhone 6s上运行时,绿色和青色视图与背景图像的白色三角形几乎在y位置。但是,当在iPhone X上运行时,这两个视图比白色三角形看起来更靠上。
当我在iPhone X上运行时,我希望使对象在其他设备上的位置相同。
我怎么解决这个问题?您是否有更好的想法来调整布局?
class Constants {
//width of base design size
static let guiPartsWidthOnDesign = CGFloat(750.0)
//ratio for layout
static var guiPartsMultiplier: CGFloat = UIScreen.main.bounds.width / (Constants.guiPartsWidthOnDesign / 2.0)
//detect safe area
static let safeArea = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x: 0,
y: 0,
width: self.view.frame.size.width,
height: self.view.frame.size.height)
//backgrond
let background = UIImageView.init(frame: UIScreen.main.bounds)
background.center.x = self.view.frame.size.width/2
background.image = UIImage(named:"BG")
background.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(background, at: 0)
//downleft blue button
let blueButton = UIImageView.init(frame: CGRect(
x: 64/2*Constants.guiPartsMultiplier,
y: self.view.frame.size.height-(52/2+34/2)*Constants.guiPartsMultiplier-Constants.safeArea!,
width: 60/2*Constants.guiPartsMultiplier,
height: 52/2*Constants.guiPartsMultiplier))
self.view.addSubview(blueButton)
blueButton.backgroundColor = UIColor.blue
//green button
let greenButton = UIImageView.init(frame: CGRect(
x: (60/2+64/2+128/2)*Constants.guiPartsMultiplier,
y: self.view.frame.size.height-(52/2+34/2)*Constants.guiPartsMultiplier-Constants.safeArea!,
width: 60/2*Constants.guiPartsMultiplier,
height: 52/2*Constants.guiPartsMultiplier))
self.view.addSubview(greenButton)
greenButton.backgroundColor = UIColor.green
//cyan button
let cyanButton = UIImageView.init(frame: CGRect(
x: (64/2+(60/2)*2+(128/2*2))*Constants.guiPartsMultiplier,
y: self.view.frame.size.height-(52/2+34/2)*Constants.guiPartsMultiplier-Constants.safeArea!,
width: 60/2*Constants.guiPartsMultiplier,
height: 52/2*Constants.guiPartsMultiplier))
self.view.addSubview(cyanButton)
cyanButton.backgroundColor = UIColor.cyan
//4 blue buttons
for i in 1..<5 {
let subBlueButton = UIImageView.init(frame: CGRect(
x: 64/2*Constants.guiPartsMultiplier,
y: self.view.frame.size.height-((43.0+CGFloat(50*i))*Constants.guiPartsMultiplier)-Constants.safeArea!,
width: 60/2*Constants.guiPartsMultiplier,
height: 52/2*Constants.guiPartsMultiplier))
self.view.addSubview(subBlueButton)
subBlueButton.alpha = 1.0
subBlueButton.backgroundColor = UIColor.blue
}
//down bar
let bar = UIImageView.init(frame: CGRect(
x:0,
y: self.view.frame.size.height-50,
width: self.view.frame.size.width,
height: 50))
bar.alpha = 0.3
bar.backgroundColor = UIColor.blue
self.view.addSubview(bar)
print("Constants: \(UIScreen.main.bounds.size.width, Constants.guiPartsWidthOnDesign, Constants.guiPartsMultiplier)")
print("SafeArea: \(String(describing: Constants.safeArea))")
print("Height: \(UIScreen.main.bounds.size.height)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您需要在情节提要中或使用视觉格式语言的代码中使用自动版式。
Ray Wenderlich的本教程介绍了如何使用VFL Ray Wnderlich Visual Format Language
Ray Wederlich Autolayout in Storyboard Tutorial
AutoLayout的目标是完全按照您的要求进行操作。