0

I have a UIScrollView that contains a view derived from UIView that uses CATiledLayer. Essentially, in my ViewController viewDidLoad:

_tiledView = [[TiledView alloc] initWithFrame:rect tileSize:_tileSize];

_scrollView = [[ScrollingView alloc] initWithFrame:rect];
_scrollView.contentSize = _tiledView.frame.size;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
_scrollView.scrollEnabled = YES;
_scrollView.delegate = self;

[_scrollView addSubview:_tiledView];

Initially, _tiledView is a 4x4 grid of 256x256 tiles. I'm trying increase the dimensions of _tiledView at runtime. When constructing _tiledView, I simply compute the size of the view by multiplying the number of tiles by its size. Then I set the size of _tiledView.frame and _tiledView.bounds, e.g.:

CGRect frame = self.frame;
frame.origin = CGPointZero;
frame.size = CGSizeMake(tileSize.width*4, tileSize.height*4);
self.frame = frame;
self.bounds = frame;

Now, when I hit a button in the interface, all I want to accomplish as a first step is increasing the dimensions of _tiledView by one 256x256 tile to both right and bottom. This is what I attempted:

- (void)addTiles:(id)sender
{
    CGRect rect = _tiledView.frame;
    rect.size.width += _tileSize.width;
    rect.size.height += _tileSize.height;
    _tiledView.frame = rect;
    _tiledView.bounds = rect;
    CGSize size = _scrollView.contentSize;
    size.width += _tileSize.width;
    size.height += _tileSize.height;
    _scrollView.contentSize = size;
    [_scrollView setNeedsLayout];
}

However, this doesn't work as expected. What happens is that _tiledView gets bigger as if it had been zoomed in - same number of tiles as the beginning though, i.e. 4x4. I checked the _scrollView.contentsScaleFactor property and it says 1.0. I assume _scrollView did not technically zoom the contents in.

I was expecting _tileView to stay put in its current place in the interface but add 9 new tiles, i.e. 4 to the right, 4 at the bottom and 1 at the bottom-right corner. Instead, the initial 16 tiles got bigger to fill in the space that could have been filled by 25 tiles.

What am I missing? What am I doing wrong? Any help would be appreciated.

Luis Artola
  • 771
  • 6
  • 20

1 Answers1

0

In case anyone finds it useful. After digging further, I realized that I the contentMode defaults to ScaleToFill. So I set it to:

_tiledView.contentMode = UIViewContentModeRedraw;

upon initialization. And adjusted addTiles like this:

CGRect rect = _tiledView.frame;
rect.size.width += _tileSize.width;
rect.size.height += _tileSize.height;
_tiledView.frame = rect;
_tiledView.bounds = rect;
_scrollView.contentSize = rect.size;
[_tiledView setNeedsDisplay];

And, that had the effect I was looking for.

Luis Artola
  • 771
  • 6
  • 20