UIScrollView change contentOffset when changing frame

I am having some problems changing the scope of my scrollview ... sometimes this change causes a scroll, and sometimes not.

Here is the situation. I have a custom UIScrollView as a subtask of my root view controller. In the parent view controller, I have a button to change the scrollview frame. It changes between 2/3 of the screen and full screen. This frame change never causes a change in the scroll contentOffset.

In scrollview content, I have some buttons. These buttons also change the scrollview frame. But sometimes this change causes scrollview to change its contentOffset ...

I cannot understand why the button inside the scroll causes this ...

I already tried calling the delegate when I clicked the button (and changing the scrollview frame in the parent view controller). And tried to change the frame inside scrollview (when the button is clicked). But the problem is the same.

Any idea what is happening or some kind of workaround for this situation?

Thank!


I tried to isolate the problem to give a simple code example here.

In my xib there is only a scrollview with a frame ((20,44), (728,300)) and a button that raises the "btnOut" event. This scrollview is disabled using Paging Enabled and Autoresize Subviews (others by default).

I will use a boolean to find out if the scrollview is large or not (bigScroll).

Here is my viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // the initial frame of scrollView is the big frame
    _bigScroll = YES;

    // here I put a big content to create some pages in scrollView
    [_scrollView setContentSize:CGSizeMake(2184, 300)];

    // I put one button in each column
    // When scrollView is with big frame it shows two columns and when it is with small frame it shows only one column.

    UIButton *btn01 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn01 setFrame:CGRectMake(159, 30, 46, 30)];
    [btn01 setTitle:@"01" forState:UIControlStateNormal];
    [btn01 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn01];

    UIButton *btn02 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn02 setFrame:CGRectMake(523, 30, 46, 30)];
    [btn02 setTitle:@"02" forState:UIControlStateNormal];
    [btn02 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn02];

    UIButton *btn03 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn03 setFrame:CGRectMake(887, 30, 46, 30)];
    [btn03 setTitle:@"03" forState:UIControlStateNormal];
    [btn03 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn03];

    UIButton *btn04 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn04 setFrame:CGRectMake(1251, 30, 46, 30)];
    [btn04 setTitle:@"04" forState:UIControlStateNormal];
    [btn04 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn04];

    UIButton *btn05 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn05 setFrame:CGRectMake(1615, 30, 46, 30)];
    [btn05 setTitle:@"05" forState:UIControlStateNormal];
    [btn05 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn05];

    UIButton *btn06 = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn06 setFrame:CGRectMake(1979, 30, 46, 30)];
    [btn06 setTitle:@"06" forState:UIControlStateNormal];
    [btn06 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
    [_scrollView addSubview:btn06];
}

Both actions "btnIn" and "btnOut" call the same function (changeFrameScrollView).

- (void)changeFrameScrollView {
    _bigScroll = !_bigScroll;

    if (_bigScroll) {
        [_scrollView setFrame:CGRectMake(20, 44, 728, 300)];
    } else {
        [_scrollView setFrame:CGRectMake(20, 44, 364, 300)];
    }
}

: , , , .

  • , .
  • , contentOffset.

** , .

: , , contentOffset.

  • scrollview , 02, , 03 04 ( 02 03).

... , , .

+3
2

, , = = _adjustContentOffsetIfNecessary

. .

, , UIScrollView touchesShouldBegin ( scrollView)

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    [self setPagingEnabled:NO];
    return YES;
}

, scrollViewWillBeginDragging.

, contentOffset , scrollView .

+1

:

,

contentSize.height   |  200 
frame.size.height    |  100
contentOffset.y      |    0

. frame

frame.size height    |  200

contentOffset .

, ,

contentSize.height   |  200 
frame.size.height    |  100
contentOffset.y      |  100  

, , ,

contentOffset.y      |    0

, 100 0.

-1

All Articles