简体   繁体   中英

UIView frame reset automatically while scrolling

A UIScrollView contains 5 UIView s. Basically I want to expand and collapse them, when user touches any one of UIView .

Code for animating globalPressReleasesView to expand:

            [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];

Other xib properties:

each UIView clipped scrollview.autoresize = YES

Issue:

'globalPressReleasesView' expands perfectly, but when I scroll my scrollview . globalPressReleasesView frame reset itself to original frame values defined in xib.

Can somebody guess what the issue is?

First, let's simplify the code a little:

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];

Now it's very clear what the code does.

The code appears to be absolutely correct. I would recommend you to check to what values are you setting the frames (either NSLog or breakpoint).

Note there are only two ways a view frame can be changed. 1. You change it in code 2. It's autoresized because you have changed it's ancestor's frame (or when the user's switches portrait/landscape mode).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM