ios 缩放 UIView 及其所有子项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14697714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scale UIView and all its children
提问by andreas
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.
我有一个带有大约 50 个 UIButton 的 UIView。所有按钮位置都以像素为单位,相对于我的主 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.
视图中使用的所有(背景)图像都具有更高的分辨率。当我将我的应用程序从 iPhone 移植到 iPad 时,我想增加 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?
现在我正在寻找一种将整个 UIView 放大 2* 倍的方法。在不破坏内部元素的位置的情况下这可能吗?
FYI, the UIView is designed in a NIB-file in XCode. But I don't mind if it can be done programmatically.
仅供参考,UIView 是在 XCode 的 NIB 文件中设计的。但我不介意它是否可以以编程方式完成。
回答by andreas
I ended up using
我最终使用
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
It allowed me to keep the design created in the Interface Builder.
它允许我保留在 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.
不幸的是,在这种情况下图像的清晰度会受到影响,但与以编程方式编写整个设计的脚本相比,这是一个很小的代价。
回答by sahiljain
Update for Swift 3.0
Swift 3.0 更新
self.view.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
回答by huync
For Swift 4.0, zoom 2x:
对于 Swift 4.0,缩放 2 倍:
myView.transform = CGAffineTransform.identity.scaledBy(x: 2, y: 2)
回答by Danny Lin
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
您可以首先以编程方式创建这些按钮,例如使用该CGRectMake
方法创建这些按钮并声明宽度和高度为 X 和 Y 并乘以 2 如果检测到 ipad 作为原点它也应该分别更改,如果太接近可能会导致重叠彼此
Edit: It all depends on your logic, im unsure too
编辑:这完全取决于你的逻辑,我也不确定