gpt4 book ai didi

ios - 旋转后保持 subview 相对位置

转载 作者:行者123 更新时间:2023-11-29 03:31:31 24 4
gpt4 key购买 nike

Figure 1

我的应用程序构建在具有 UIImageView subview 和各种其他 subview 的 UIScrollview 上。当我旋转到横向时,我更改 ScrollView 的 contentSize 并按比例调整 ImageView 的大小以利用增加的水平宽度。这意味着高度也会增加以保持比例。

我的问题是,在显示的蓝色 subview 的情况下,我需要做什么来重新定位它,以便在旋转后保持它的相对位置,因为它的父 View 不再是相同的大小?我已经尝试过 convertRect:toView: 和 converPoint:toView:,但我似乎不太正确。

最佳答案

您使用自动布局吗?如果是这样,在许多情况下,明智地使用约束可以使 subview 保持在正确的位置和正确的大小,即使您从横向转到纵向也是如此。但您必须分享有关此 View 中其他内容的更多详细信息,以便我们更加具体。

如果不使用自动布局,您通常可以设置 View 的自动调整大小蒙版,以便它移动到您的正确位置。但在 ScrollView subview 的情况下,您的 View Controller 可能可以响应 viewWillLayoutSubviews ,更新 ScrollView 的 contentSize 和要移动的 subview 的 frame,相应地:

- (void)viewWillLayoutSubviews
{
// update the contentSize of the scroll view for the width of the root view, but I'm assuming the
// height won't change

self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.scrollView.contentSize.height);

// adjust the frame of the subview you want to move so that it is a certain offset from the bottom
// left corner of the scroll view's `contentSize` (in this case, 10 points from bottom, 10 points from right)

self.subviewToMove.frame = CGRectMake(self.scrollView.contentSize.width - self.subviewToMove.frame.size.width - 10,
self.scrollView.contentSize.height - self.subviewToMove.frame.size.height - 10,
self.subviewToMove.frame.size.width, self.subviewToMove.frame.size.height);
}

具体情况因以下细节而异:(a) 您是否使用自动布局; (b) 是否以编程方式创建此 subview ; (c) 您的 View 中还有哪些其他内容,以及从纵向到横向再返回的更改是否会导致 ScrollView 的垂直高度发生任何变化。

关于ios - 旋转后保持 subview 相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com