mercredi 6 juillet 2016

Scaling on iPhone / iPad with percentages

This is the way I add buttons and labels with images to my views.

Currently I have problems with scaling on iPad. I need a quick and nice solution to scale my images in the correct ratio.

Currently my images mostly are too wide on iPad. I also only use 1x scale factor for my images (sometimes it's enough to set the contentMode of imageViews to scaleAspectFill).

How can I solve this issue, regarding the way I currently add images? Should I use UIUserInterfaceIdiom to detect if I have an iPad or iPhone to determine different positions and bounds?

func initbutton()
{
  let buttonX: CGFloat = 0.156
  let buttonY: CGFloat = 0.734
  let buttonWidth: CGFloat = 0.688
  let buttonHeight: CGFloat = 0.076

  button = UIButton(frame: CGRectMake(buttonX * view.frame.width,
                                             buttonY * view.frame.height,
                                             buttonWidth * view.frame.width,
                                             buttonHeight * view.frame.height))

  button.setBackgroundImage(UIImage(named: "button.png")!)
  view.addSubview(button)
}

Aucun commentaire:

Enregistrer un commentaire