Monthly Archive for July, 2010

A infinite/endless paging UIScrollView

Infinity is a very powerful concept, so maybe my post title shouldn’t include this term. Anyway, I tried to create something similar to a infinite or endless paging scroll view.

The idea is very simple:

  • Create a view including a standard scroll view.
  • Provide an interface to load views for each page on demand.
  • Cache views for pages (specify the maximum amount of cached pages).
  • If user scrolls the view load (and cache) the next and previous view.

You will find the source code of this component (I named it InfinitePagingView) and a example project at our github repository.

Using InfinitePagingView is very straightforward. Just implement the InfinitePagingDataSource protocol, create an instance of InfinitePagingView and add it to your current view:

InfinitePagingView *infinitePagingView = [[InfinitePagingView alloc]
            initWithFrame:CGRectMake(0, 0, 320, 480)
            andDataSource:self];
 
[self.view addSubview:infinitePagingView];
[infinitePagingView release];

After that -(UIView*) infinitePagingView:(InfinitePagingView*)infinitePagingView viewForPageIndex:(int)index gets called for each page and you can provide your custom view for the requested page.

Cheers,
Andreas

Transparent UIToolBar

Sometimes it is really necessary to have a transparent UIToolBar within you iPhone app. Especially if you want to add a UIToolBar to your UINavigationBar you get some problems with overlaying backgrounds (UIBarStyleBlackOpaque did not work for a toolbar on a black opaque navigation bar).

A quick and pragmatic solution is to subclass UIToolBar and do some modifications. The following code is everything you need to get a really transparent tool bar:

@interface TransparentToolbar : UIToolbar
@end
 
@implementation TransparentToolbar
 
// Override draw rect to avoid
// background coloring
- (void)drawRect:(CGRect)rect {
    // do nothing in here
}
 
// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
{
	self.backgroundColor = [UIColor clearColor];
	self.opaque = NO;
	self.translucent = YES;
}
 
// Override init.
- (id) init
{
	self = [super init];
	[self applyTranslucentBackground];
	return self;
}
 
// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
	self = [super initWithFrame:frame];
	[self applyTranslucentBackground];
	return self;
}
 
@end

To apply a color to the including UIBarButtonItems you can set the bar style as usual.

That’s all for today,
Andreas