62

I have an UIView with around 50 UIButtons. All button positions were given in pixels, relative to the left upper corner of my main UIView.

All (background) images used in the view are available in higher resolution. As I am porting my app from iPhone to iPad, I would like to increase the effective pixel size of the UIView.

Now I'm searching a way to upscale the whole UIView by a factor of 2*. Is that possible without destroying the position of the inner elements?

FYI, the UIView is designed in a NIB-file in XCode. But I don't mind if it can be done programmatically.

andreas
  • 7,844
  • 9
  • 51
  • 72
  • as in you want the buttons to remain in their original positions or to stretch with the parent view? – KDaker Feb 04 '13 at 23:42
  • 4
    50 buttons? Sounds like an end-user's nightmare. Are you trying to make a calculator? – Richard J. Ross III Feb 04 '13 at 23:47
  • 2
    The buttons have to upscale too. So their width and height has to double as their parent is doubling too. -- Don't worry, it's not a user's nightmare. The app shows a stadium, you can click each sector and get an image from there. – andreas Feb 05 '13 at 00:10
  • Just set the width and height of the button frames to the new value - this will not affect the button's origin. – Luke Feb 05 '13 at 00:49
  • 4
    not sure is this what you want `self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);` – Bryan Chen Feb 05 '13 at 01:40
  • This could work! My first attempt kept all buttons as I wanted. I will try to make it work now – andreas Feb 05 '13 at 13:36

4 Answers4

126

I ended up using

self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);

It allowed me to keep the design created in the Interface Builder.

Unfortunately the sharpness of the image suffers in that case, but this is a small price to pay compared to scripting the whole design programmatically.

andreas
  • 7,844
  • 9
  • 51
  • 72
  • 41
    Slightly easier: `self.view.transform = CGAffineTransformMakeScale(2, 2);` – hatfinch Feb 12 '15 at 23:35
  • How does this end up affecting frame of the view? I'm interested in using this same method to scale down a menu in cases of multitasking where the window is too narrow to fit the full menu. The documentation for the transform property contains the following warning. "If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored." https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/transform – Joey Carson Dec 29 '15 at 15:43
  • 1
    Just spitballing here and haven't tried yet, but perhaps instead of using the transform property directly, it's better to use CGRectApplyAffineTransform on the current frame itself, therefore the frame is updated accordingly and can still be trusted in other logic that depends on it. – Joey Carson Dec 29 '15 at 15:48
  • 1
    UIView is not being centered after applying the Scaling Transformation. Is there anything i am missing ? I want to center the UIView after scaling it down. – Salman Khakwani Mar 29 '16 at 08:43
  • Swift 4: `myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)` – huynguyen Jun 15 '18 at 16:30
13

Update for Swift 3.0

self.view.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
Community
  • 1
  • 1
sahiljain
  • 2,215
  • 1
  • 29
  • 39
13

For Swift 4.0, zoom 2x:

myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)
huynguyen
  • 7,616
  • 5
  • 35
  • 48
2

You can first programmatically create those buttons like example create those buttons using the CGRectMake method and stating the width and height to be X and Y and multiply by 2 if ipad is detected as for origin it should change respectively too, might cause overlapping if too close to each other

Edit: It all depends on your logic, im unsure too

Danny Lin
  • 180
  • 8
  • That is true, but I already have my buttons in the iPhone app in the NIB. I hoped there was a shorter solution than creating all buttons programmatically now. – andreas Feb 05 '13 at 01:29
  • @andreas im not sure if there is such a solution to be able to resize respectively but from what i see, creating programmatically will always be better as you can modify the codes as and when you wanted to – Danny Lin Feb 05 '13 at 01:36
  • I agree. The reason I decided to do it graphically was the difficulty of perfect alignment. It was easier using the Interface Builder. – andreas Feb 05 '13 at 13:34