カテゴリの代わりに、subclass
UITabBarController
. これにより、オブジェクトをより細かく制御できます。サブクラスの例を次に示します。
// MPCustomTabBar.h
@interface MPCustomTabBar : UITabBarController
- (void) setBackgroundImage:(UIImage *)image;
@end
// MPCustomTabBar.m
@interface MPCustomTabBar
- (void) setBackgroundImage:(UIImage *)image {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.backgroundColor = [UIColor colorWithPatternImage:i];
[[self view] addSubview:imageView];
[[self view] sendSubviewToBack:imageView];
[[self view] setOpaque:NO];
[[self view] setBackgroundColor:[UIColor clearColor]];
[imageView release];
}
@end
これで、次のようにして、必要なすべてのカスタマイズを行い、新しいサブクラスを割り当てて初期化できます。
MPCustomTabBar *bar = [[MPCustomTabBar alloc] init];