简体   繁体   English

我如何使用StackView中设置为按比例填充的子视图的属性,因为它在旋转设备后会重新计算

[英]How do I use attributes of subviews within StackView set to Fill Proportionally as it recalculates after rotating device

I have implemented a stack view filled proportionally with images and labels and text and buttons inside of a popover over my primary view controller. 我实现了一个堆栈视图,该视图在我的主视图控制器上方的弹出窗口中按比例填充了图像,标签,文本和按钮。 When I segue to the popover I use the view will appear override to define the corner radius of the image that I call ProfileImage to be 1/2 its height so that it appears as a circle. 当我选择弹出窗口时,使用的视图将显示为Override,以将我称为ProfileImage的图像的角半径定义为其高度的1/2,以使其显示为圆形。

I when I rotate the device, the stack recalculates and changes the size of the profileImage. 我旋转设备时,堆栈会重新计算并更改profileImage的大小。 I have attempted to use the code below to adjust the corner radius so that it is a circle when the view transitions. 我试图使用下面的代码来调整拐角半径,以便在视图过渡时它成为一个圆。 I have tried this code in both the completion block and in the alongsideTransition block. 我已经在完成块和sidesideTransition块中尝试了此代码。

When I rotate the device the corner radius is set by the size of the image in the OLD orientation, not in the NEW orientation. 当我旋转设备时,转角半径由旧方向(而不是新方向)上的图像尺寸设置。 It feels as though the transition to new orientation happens and THEN the stack recalculates itself. 感觉好像发生了向新方向的过渡,然后堆栈重新计算了自己。 The corner radius seems to update before this happens. 在此之前,拐角半径似乎已更新。 I would like help understanding how to call the height property of image that will be the height in the new view after the device rotates and the stack recalculates the image size based on that new orientation. 我想帮助您了解如何调用图像的height属性,该属性将在设备旋转并且堆栈基于该新方向重新计算图像大小后在新视图中成为高度。

Thank you for any guidance you can provide. 感谢您提供的任何指导。 Please see below for the code in question: 请参阅以下有关代码:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: { context in
        context.viewController(forKey: UITransitionContextViewControllerKey.from)
    }, completion: {(context) -> Void in
         self.view.layoutSubviews()
        self.ProfileImage.layer.cornerRadius = self.ProfileImage.bounds.size.height / 2
        print(self.ProfileImage.frame.size.height)
    })  
}

An answer that is close but isn't perfect is to set the corner radius in viewWillLayoutSubviews() instead of in viewWillTransition() because the UIStack view lays out its subviews after the rotation not during it. 一个接近但不完美的答案是在viewWillLayoutSubviews()中设置拐角半径,而不是在viewWillTransition()中设置拐角半径,因为UIStack视图在旋转之后而不是在旋转期间布置其子视图。

override func viewWillLayoutSubviews() {
   self.ProfileImage.layer.cornerRadius = self.ProfileImage.frame.size.height / 2 
}

thank you to the respondents to this article for pointing me in the right direction: View frame changes between viewWillAppear: and viewDidAppear: 感谢本文的答复者为我指明了正确的方向: viewWillAppear:和viewDidAppear之间的视图框架变化:


My issue now is that somehow the size of the UIImage view has changed by a few pixels after the viewDidLayoutSubviews Call. 我现在的问题是,在viewDidLayoutSubviews调用之后,UIImage视图的大小已以某种方式改变了几个像素。 Is there another function that would be called when a stack view or autolayout recaculates after the DidLayoutSubviews? 在DidLayoutSubviews之后重新显示堆栈视图或自动布局时,还会调用另一个函数吗?

The app looks fine but there is some other behavior happening that I don't understand. 该应用程序看起来不错,但有一些我不理解的其他行为正在发生。

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

相关问题 如何在子视图中叠加子视图? - How to Overlap subViews in a stackView? 我如何设置另一个分布均匀填充的另一个stackview内部的stackview的高度 - How can i set the height of stackview present inside another stackview whose distribution is fill equally distribution 如何为嵌入在垂直堆栈视图中的水平 uistackviews 设置正确的 alignment? - How do I set up right alignment for horizontal uistackviews embedded within a vertical stackview? 在按比例调整子视图的大小时,如何调整视图的大小? - How can I resize a view, while proportionally resizing it's subviews? 使用带有对齐(填充)和分布(按比例填充)的嵌套垂直 StackView 渲染 ScrollView 内容的问题 - Problem rendering ScrollView content with a nested vertical StackView with Alignment(Fill) and Distribution(Fill Proportionally) 如何在滚动视图的顶部创建一个stackview? - How do I make a stackview at the top of a scrollview? 在StackView中对子视图进行动画处理? - Animating subviews in a stackview? 如何根据第一个标签的宽度设置水平stackView - How can I set the horizontal stackView according to the width of the first label 如何以编程方式将自定义布局添加到 StackView 或 TableView? - How do I programmatically add a custom layout to a StackView or TableView? 在segue中如何访问子视图 - How do I access subviews during a segue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM