Scale UIView and all its children
Asked Answered
O

4

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.

Ogawa answered 4/2, 2013 at 23:34 Comment(6)
as in you want the buttons to remain in their original positions or to stretch with the parent view?Eshelman
50 buttons? Sounds like an end-user's nightmare. Are you trying to make a calculator?Arbitrament
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.Ogawa
Just set the width and height of the button frames to the new value - this will not affect the button's origin.Hayse
not sure is this what you want self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);Perforce
This could work! My first attempt kept all buttons as I wanted. I will try to make it work nowOgawa
O
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.

Ogawa answered 5/2, 2013 at 23:34 Comment(5)
Slightly easier: self.view.transform = CGAffineTransformMakeScale(2, 2);Ido
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." developer.apple.com/library/ios/documentation/UIKit/Reference/…Chunk
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.Chunk
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.Subcutaneous
Swift 4: myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)Signor
C
13

Update for Swift 3.0

self.view.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
Calcaneus answered 12/7, 2017 at 8:24 Comment(0)
S
13

For Swift 4.0, zoom 2x:

myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)
Signor answered 15/6, 2018 at 16:29 Comment(0)
P
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

Planetarium answered 5/2, 2013 at 1:26 Comment(3)
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.Ogawa
@Ogawa 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 toPlanetarium
I agree. The reason I decided to do it graphically was the difficulty of perfect alignment. It was easier using the Interface Builder.Ogawa

© 2022 - 2024 — McMap. All rights reserved.