UIView 框架在滚动时自动重置

Posted

技术标签:

【中文标题】UIView 框架在滚动时自动重置【英文标题】:UIView frame reset automatically while scrolling 【发布时间】:2013-03-29 13:07:38 【问题描述】:

一个UIScrollView 包含5 个UIViews。基本上,当用户触摸UIView 中的任何一个时,我想展开和折叠它们。

动画globalPressReleasesView展开代码:

            [UIView beginAnimations:@"Expand" context:nil];
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationDelegate:self];

                                [globalPressReleasesView setFrame:CGRectMake(globalPressReleasesView.frame.origin.x, globalPressReleasesView.frame.origin.y, globalPressReleasesView.frame.size.width, globalPressReleasesView.frame.size.height + [globalPressReleasesArray count]*105)];

                                [financialPressReleasesView setFrame:CGRectMake(financialPressReleasesView.frame.origin.x, financialPressReleasesView.frame.origin.y + [globalPressReleasesArray count]*105, financialPressReleasesView.frame.size.width, financialPressReleasesView.frame.size.height)];


                                [newOnCscView setFrame:CGRectMake(newOnCscView.frame.origin.x, newOnCscView.frame.origin.y + [globalPressReleasesArray count]*105, newOnCscView.frame.size.width, newOnCscView.frame.size.height)];

                                [latestEventsView setFrame:CGRectMake(latestEventsView.frame.origin.x, latestEventsView.frame.origin.y + [globalPressReleasesArray count]*105, latestEventsView.frame.size.width, latestEventsView.frame.size.height)];

                                [latestCaseStudiesView setFrame:CGRectMake(latestCaseStudiesView.frame.origin.x, latestCaseStudiesView.frame.origin.y + [globalPressReleasesArray count]*105, latestCaseStudiesView.frame.size.width, latestCaseStudiesView.frame.size.height)];

                                [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height + [globalPressReleasesArray count]*105)];

            [UIView commitAnimations];

其他 xib 属性:

每个UIView 被剪辑 scrollview.autoresize = YES

问题:

'globalPressReleasesView' 完美扩展,但是当我滚动我的scrollview 时。 globalPressReleasesView 框架将自身重置为 xib 中定义的原始框架值。

谁能猜出问题出在哪里?

【问题讨论】:

一次检查滚动视图委托方法 是的,试过了,这就是我知道的,所有包含UIViews 帧的值正在重置。 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 在这个方法中检查 globalPressReleasesView 框架? 经过同样的尝试,我得出了这个结论。 globalPressReleasesView 帧值会自动替换为以前的值。在第一次暂停到断点时,它会打印扩展的帧值,但在第二次停止时,它会返回旧的帧值。这就是每个视图返回到其旧位置的方式。 【参考方案1】:

首先,让我们稍微简化一下代码:

CGFloat heightDifference = [globalPressReleasesArray count] * 105.0f;

CGRect globalPressReleasesViewFrame = globalPressReleasesView.frame;
globalPressReleasesViewFrame.size.height += heightDifference;
globalPressReleasesView.frame = globalPressReleasesViewFrame;

NSArray* movedViews = @[financialPressReleasesView, newOnCscView, latestEventsView, latestCaseStudiesView];

for (UIView* movedView in movedViews) 
   CGRect movedViewFrame = movedView.frame;
   movedViewFrame.origin.y += heightDifference;
   movedView.frame = movedViewFrame


CGSize contentSize = scrollView.frame.size;
contentSize.height += heightDifference;
[scrollView setContentSize:contentSize];

现在很清楚代码的作用了。

代码似乎完全正确。我建议您检查设置帧的值(NSLog 或断点)。

请注意,只有两种方式可以更改视图框架。 1.你在代码中改变它 2. 它是自动调整大小的,因为你改变了它的祖先框架(或者当用户切换纵向/横向模式时)。

【讨论】:

我已经在滚动视图委托上设置了断点,以确定 UIView 何时更改。当我开始滚动 UIScrollView 时,UIView 框架的第一个值是好的,然后它会自行更改为以前的值。我可以在其他地方设置断点来完成这项工作吗? 我改变了编码方式。我把那些UIViews 当作UITableView 的部分,tableview 属性本身控制着一切.. :)

以上是关于UIView 框架在滚动时自动重置的主要内容,如果未能解决你的问题,请参考以下文章

向下滚动并返回时 UITextField 进入 UITableViewCell 自动重置?

滚动其他滚动视图时滚动视图重置

如何在滚动视图中重置文本字段

在代码中更改框架后自动布局重置视图

自动布局位置重置

当 UISearchController 获得焦点时重置 UIView