我想让 UIButton 适合所有 iPhone 屏幕:
- iPhone 4s
- iPhone 5
- iPhone 5s
- iPhone 6s Plus
如何计算与屏幕尺寸相关的距离?
我需要为所有 iPhone 屏幕安装暂停按钮和录制按钮。
实际上,它在 iPhone 6 上看起来很完美,但在 iPhone 6 plus 上看起来非常不同。
我没有使用 Storyboard!我只需要用代码来做这件事非常重要!
iPhone 6:
iPhone 6 Plus :
游戏 View Controller :
暂停按钮:
func addPauseButton() {
//create Pause Button
PauseButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 150,self.view!.frame.size.height/2 - 315 ,30,30))
PauseButton.setImage(UIImage(named: "Pause-Button.png"), forState: UIControlState.Normal)
PauseButton.addTarget(self, action: "pressedPauseGame:", forControlEvents: .TouchUpInside)
self.view.addSubview(PauseButton)
}
录制按钮:
func addRecordButton() {
startRecordingButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 155 ,self.view!.frame.size.height/2 - 275 ,25,25))
startRecordingButton.setImage(UIImage(named: "Record-Off.png"), forState: UIControlState.Normal)
startRecordingButton.addTarget(self, action: "startRecordingButtonTapped:", forControlEvents: .TouchUpInside)
self.view.addSubview(startRecordingButton)
stopRecordingButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 + 155 ,self.view!.frame.size.height/2 - 275 ,25,25))
stopRecordingButton.setImage(UIImage(named: "Record-On.png"), forState: UIControlState.Normal)
stopRecordingButton.addTarget(self, action: "stopRecordingButtonTapped:", forControlEvents: .TouchUpInside)
self.view.addSubview(stopRecordingButton)
}
最佳答案
“StoryBoard”是您的 friend ,现在花点时间学习如何使用 StoryBoard 以在将来为自己节省大量时间,请参见下图,所有按钮在所有设备上都位于同一位置,零编码工作。有点绝妙的想法,一些编码中的老定时器没有使用,(或者编码器太习惯于对计算机屏幕上的每个 x,y 点进行编码)
不幸的是,一些老前辈进入了 Apple,然后我们失去了一些东西......比如 xCode 快照......真的太糟糕了,所以使用它或丢失它。
关于ios - 如何计算与窗口大小相关的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560309/