简体   繁体   English

iOS统一比例子视图与superview

[英]iOS Uniform scale subviews with superview

I have an app that allows a user to drag views onto the screen in multiple orientations. 我有一个应用程序,允许用户以多个方向将视图拖动到屏幕上。 So lets say i'm in portrait mode and my superview is 768x1024. 所以,让我说我是在纵向模式,我的超级视图是768x1024。 Lets say I drag 10 different items onto that superview. 让我们说我将10​​个不同的项目拖到超级视图上。 These items are scaled, rotated, etc. Then I rotate the device and my superview shrinks down to 576x768. 这些项目是缩放,旋转等。然后我旋转设备,我的超级视图缩小到576x768。 How do I get all of my subviews to maintain their position and transform relative to the new smaller size? 如何让我的所有子视图保持其位置并相对于新的较小尺寸进行转换? So in essence my superview and all of its subviews should look exactly as they did while in portrait, except everything has been scaled down. 所以从本质上讲,我的超级视图及其所有子视图应该看起来与他们在肖像中所做的完全一样,除了所有内容都按比例缩小。

And also, what if im in landscape and everything is "shrunk" down and then I drag another view on and shrink it down to fit well. 而且,如果我在景观和一切都“缩小”下来然后我拖动另一个视图并缩小它以适应好。 I would need that new view to scale up in proper place and size when the device is rotated back into portrait 当设备旋转回肖像时,我需要新视图在适当的位置和大小放大

The easiest thing to do here is to use the transform property of your view. 这里最简单的方法是使用视图的transform属性。 In this case, you'd prevent the view from auto-resizing on rotation, and use something like your view controller's willAnimateRotationToInterfaceOrientation:duration: method to set a transform on your superview. 在这种情况下,您将阻止视图在旋转时自动调整大小,并使用类似视图控制器的willAnimateRotationToInterfaceOrientation:duration:方法在willAnimateRotationToInterfaceOrientation:duration:上设置变换。 You can create a transform with CGAffineTransformMakeScale that will scale up or down all of your content; 您可以使用CGAffineTransformMakeScale创建一个可以放大或缩小所有内容的转换; you'll probably want one for landscape and just use a nil transform for portrait (or vice-versa). 你可能想要一个用于风景,只需使用nil变换用于肖像(反之亦然)。

This will complicate dragging new views into your superview, since it's transformed, but once you've got views in there, they should behave correctly when resizing. 这会将新视图拖动到您的超级视图中变得复杂,因为它已被转换,但是一旦您获得了视图,它们在调整大小时应该正常运行。 For new views, you'll have to compute (perhaps using convertRect:fromView: method of UIView) what a rect will be once you add a new subview. 对于新视图,您必须计算(可能使用convertRect:fromView:UIView的方法)一旦添加新的子视图,rect将是什么。

You can also override your view's layoutSubviews() directly and assign your subview's transforms based on your view's dimension. 您还可以直接覆盖视图的layoutSubviews() ,并根据视图的尺寸分配子视图的变换。 This way, you can still get the benefits of using Auto Layout with your view and potentially other sibling UI items, while you customize subview's frames and/or transforms manually without Auto Layout. 通过这种方式,您可以在不使用自动布局的情况下手动自定义子视图的框架和/或变换,从而获得在视图和其他同级UI项目中使用自动布局的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ( iOS ) 什么是 superview 什么是 subviews - ( iOS ) What is superview and what is subviews 在 iOS 上,如果超级视图的 userInteractionEnabled 为 NO,那么所有子视图也会被禁用? - On iOS, if a superview's userInteractionEnabled is NO, then all subviews are disabled as well? (iphone)取消分配超级视图后,子视图会怎样? - (iphone) what happens to subviews when superview is dealloced? 隐藏超级视图而不隐藏其子视图 - Hiding a superview without hiding its subviews 在iPad中删除超级视图时,子视图未删除 - Subviews are not removing while removing the superview in iPad 禁用超级视图用户交互时,子视图用户交互 - subviews userinteraction when superview userinteraction disabled swift 可以让superView拦截layoutSubviews:来自子视图吗? - Possible to make superView intercept layoutSubviews: from subviews? iPhone iOS如何让UIRotationGestureRecognizer和UIPinchGestureRecognizer一起工作来扩展和旋转带有子视图的UIView? - iPhone iOS how to make UIRotationGestureRecognizer and UIPinchGestureRecognizer to work together to scale and rotate a UIView with subviews? 缩放 UIView 而不缩放子视图 - Scale UIView without scaling subviews 如何在超级视图的顶部添加子视图的视图? - How can I add a view from subviews on top of its superview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM