2016-02-22 35 views
1

我想适应的UIButton所有iPhone屏幕的大小,距离:如何关系计算到窗口

  • iPhone 4S
  • iPhone 5
  • iPhone 5S
  • iPhone 6S加

我如何计算距离与屏幕尺寸的关系?

我需要适合所有iPhone屏幕的暂停按钮和录制按钮。

实际上,它在iPhone 6上看到完美,但在iPhone 6上加上它看起来非常不同。

我不使用故事板!我只需要使用代码就非常重要!

iPhone 6:

enter image description here

iPhone 6加:

enter image description here

GameViewController:

暂停按钮:

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) 

    } 
+0

你说你不能使用自动布局,因为Sprite Kit(阅读评论)? https://github.com/mgrebenets/SpriteKitAutoLayout – tktsubota

回答

0

的“故事板”是你的朋友,花时间,现在要学习如何使用故事板保存自己的大量时间在未来,请参阅下面的图片,所有按钮,完全位于所有设备上的相同位置,无需编码。一些聪明的主意,一些编码老的计时器没有使用(或者编码器太习惯于编码计算机屏幕上的每个x,y点)

不幸的是,其中一些老计时器进入Apple,然后我们失去了东西...就像xCode快照......太糟糕了,所以使用它或丢失它。

enter image description here

+0

即时通讯不使用故事板。我正在使用sprite套件,我不能使用自动布局。我正在使用ViewController,并且我知道不用故事板就可以做到这一点 –

+0

您可以轻松地将故事板与spriteKit或任何其他套件一起使用,甚至可以完成半程序布局和半故事板,而且您还没有使用autoLayout。它可以是另一个视图内视图的内部视图,有些视图是以编程方式为屏幕上需要显示的内容而编排的。 ---有人说,如果菜单或按钮很简单,那么只需使用spritekit,但我认为即使使用一些按钮,这也可以节省您的时间/精力,只需将按钮指向代码以在spritkit中执行,然后将spritkit视图设置为在故事板视图的视图内工作。 – hokkuk

0

实际呈现的视图的大小只在遗嘱布局生效或将-出现视图控制器的回调,所以,如果你在代码中创建UI对象,您可能需要(重新)调整当前UI对象的帧rects成为您期望的视图比例(在后面的回调中,而不是接近init或启动时间的较早方法)。

当用户旋转显示器或在新iPad上对应用程序窗口大小执行其他操作时,也会出现视图过渡回调。

+0

所以我需要适应每个单独的屏幕? –

+0

每次呈现视图或更改视图时,都需要(重新)适合每个UI对象的框架。 – hotpaw2

+0

好的,你可以在我的暂停按钮上显示我的例子吗? –