0
votes

Very strange behaviour from UIScrollView. Kindly check the following code

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    scrView.delegate =self;
    [scrView setContentSize:CGSizeMake(95000, self.view.bounds.size.height)];
    scrView.backgroundColor = [UIColor clearColor];
    Testview *test = [[Testview alloc]initWithFrame:CGRectMake(0, 0, scrView.contentSize.width, self.view.bounds.size.height)];
    test.backgroundColor = [UIColor greenColor];
    [scrView addSubview:test];

    [scrView flashScrollIndicators];
    [self.view addSubview:scrView];
    self.view.backgroundColor = [UIColor clearColor];
}

Testview is a subview of Scrollview.

@implementation Testview

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];
}

In Testview class, if I call the method - (void)drawRect:(CGRect)rect, Uiscrollview goes blank and unable to view the scrollview in runtime. But if I comment drawRect method

@implementation Testview

//- (void)drawRect:(CGRect)rect {
//    
//    [super drawRect:rect];
//}

it works perfect.I am breaking my head to solve this, please help me out to solve this issue. I need drawRect to work if the content size width and subview width are more than 95000.

2
Not sure you should attempt to use -drawRect with such a large canvas. (95000x640). I guess you're probably running out of memory, but you don't give any reason for your suspicion that UIScrollView is deallocated. That's probably not what's happening. What does the debugger and crash logs tell you? - nielsbot
The app is neither crashing nor I am getting any debugger like 'Received memory warning '. Very strange it is. The View is blank in run time. - Prashanth Rajagopalan
Why do you think your view is being deallocated? Try setting the background color of your scroll view to red and the background of your test view to orange, for example... to see if they're really on screen. You could also break on -[UIScrollView dealloc] - nielsbot
Sorry for mentioning deallocated. Now I have changed to 'UIScrolview goes blank'. Actually it is not deallocated. - Prashanth Rajagopalan
ok do you see any errors at all in the log or console? Try changing your view's backing layer to CATiledLayer. (Override +layerClass in Testview to return [ CATiledLayer class ]) - nielsbot

2 Answers

1
votes

Your view is too big for ordinary -drawRect:. You should try backing your view with a CATiledLayer which serves this purpose.

0
votes

Answering to my question. If we use CATiledLayer layer you will able to use -drawRect: of UIVIew irrespective of maximum width subview given to scrollview. But -drawRect: method will be called every time a tile needs to be displayed. This piece of code helped me.

+ (Class) layerClass
{
    return [CATiledLayer class];
}