diff --git a/Classes/AXStretchableHeaderTabViewController.h b/Classes/AXStretchableHeaderTabViewController.h index 9559393..6234fc9 100644 --- a/Classes/AXStretchableHeaderTabViewController.h +++ b/Classes/AXStretchableHeaderTabViewController.h @@ -24,6 +24,9 @@ @property (weak, nonatomic) IBOutlet UIScrollView *containerView; @property (nonatomic) BOOL shouldBounceHeaderView; +// only call after view has appeared +- (void)switchToIndex:(NSUInteger)index; + // Layout - (void)layoutHeaderViewAndTabBar; - (void)layoutViewControllers; diff --git a/Classes/AXStretchableHeaderTabViewController.m b/Classes/AXStretchableHeaderTabViewController.m index 2839e9e..6b3d069 100644 --- a/Classes/AXStretchableHeaderTabViewController.m +++ b/Classes/AXStretchableHeaderTabViewController.m @@ -15,26 +15,48 @@ @implementation AXStretchableHeaderTabViewController { CGFloat _headerViewTopConstraintConstant; } -- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +//{ +// // MEMO: +// // An inherited class does not load xib file. +// // So, this code assigns class name of AXStretchableHeaderTabViewController clearly. +// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; +// if (self) { +// +// } +// return self; +//} + +- (void)switchToIndex:(NSUInteger)index { - // MEMO: - // An inherited class does not load xib file. - // So, this code assigns class name of AXStretchableHeaderTabViewController clearly. - self = [super initWithNibName:NSStringFromClass([AXStretchableHeaderTabViewController class]) bundle:nibBundleOrNil]; - if (self) { - // Custom initialization - _shouldBounceHeaderView = YES; - - _tabBar = [[AXTabBar alloc] init]; - [_tabBar setDelegate:self]; - } - return self; + if (self.viewControllers == nil + || index >= self.viewControllers.count) + return; + + self.tabBar.selectedItem = self.tabBar.items[index]; + [self.containerView setContentOffset:(CGPoint) { index *CGRectGetWidth( + self.containerView + .bounds), + self.containerView + .contentOffset + .y } animated:YES]; + self.selectedIndex = index; } - (void)viewDidLoad { [super viewDidLoad]; - + + // Custom initialization + [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([AXStretchableHeaderTabViewController class]) owner:self options:nil]; + + _shouldBounceHeaderView = YES; + + if (!_tabBar) { + _tabBar = [[AXTabBar alloc] init]; + } + + [_tabBar setDelegate:self]; [_tabBar sizeToFit]; [self.view addSubview:_tabBar]; } diff --git a/Classes/AXTabBar.m b/Classes/AXTabBar.m index 45454f0..9515a60 100644 --- a/Classes/AXTabBar.m +++ b/Classes/AXTabBar.m @@ -23,6 +23,22 @@ - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { + [self commonInit]; + } + return self; +} + +-(id)initWithCoder:(NSCoder *)aDecoder +{ + self = [super initWithCoder:aDecoder]; + if (self) { + [self commonInit]; + } + return self; +} + +-(void)commonInit +{ _tabBarButtonFont = [UIFont systemFontOfSize:14.0]; _toolbar = [[UIToolbar alloc] init]; @@ -38,11 +54,9 @@ - (id)initWithFrame:(CGRect)frame _bottomSeparator = [CALayer layer]; [_bottomSeparator setBackgroundColor:[[UIColor colorWithWhite:0.0 alpha:0.1] CGColor]]; [self.layer addSublayer:_bottomSeparator]; - + _indicatorLayer = [CALayer layer]; [self.layer addSublayer:_indicatorLayer]; - } - return self; } - (void)layoutSubviews diff --git a/Classes/AXTabBarItemButton.m b/Classes/AXTabBarItemButton.m index 393e60c..62d96da 100644 --- a/Classes/AXTabBarItemButton.m +++ b/Classes/AXTabBarItemButton.m @@ -12,11 +12,25 @@ - (id)initWithFrame:(CGRect)frame self = [super initWithFrame:frame]; if (self) { // Initialization code + [self commonInit]; + } + return self; +} + +-(id)initWithCoder:(NSCoder *)aDecoder +{ + self = [super initWithCoder:aDecoder]; + if (self) { + [self commonInit]; + } + return self; +} + +-(void)commonInit +{ [self setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected]; [self setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted]; - } - return self; } - (void)layoutSubviews