UIToolBarとUIButtonとUIScrollを組み合わせてオリジナルタブっぽいものを作る
横にスクロールするタブがほしいとか言われておいおいと思いつつなんとかつくった。
ヘッダーファイル
#import <UIKit/UIKit.h> @interface tabSampleSampleViewController : UIViewController{ IBOutlet UIToolbar *toolbar_; IBOutlet UIView *mainView_; @private UINavigationController *navigationController1_; UINavigationController *navigationController2_; UINavigationController *navigationController3_; UINavigationController *navigationController4_; UINavigationController *navigationController5_; UINavigationController *navigationController6_; UIButton *button1_; UIButton *button2_; UIButton *button3_; UIButton *button4_; UIButton *button5_; UIButton *button6_; } @property(nonatomic,retain) UIToolbar *toolbar_; @property(nonatomic,retain) UIView *mainView_; @end
実装ファイル
#import "tabSampleSampleViewController.h" #import "tableViewController.h" #import "viewController.h" #import "tabViewController.h" @implementation tabSampleSampleViewController @synthesize toolbar_,mainView_; - (void)dealloc { [toolbar_ release]; [mainView_ release]; [navigationController1_ release]; [navigationController2_ release]; [navigationController3_ release]; [navigationController4_ release]; [navigationController5_ release]; [navigationController6_ release]; [super dealloc]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle -(void) toolBarMake{ //スクロールボタンの作成 UIScrollView *scrollView = [[[UIScrollView alloc] init] autorelease]; scrollView.frame = CGRectMake(0, 0, 320, 50); scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; scrollView.backgroundColor = [UIColor clearColor]; UIView *contentView = [[[UIView alloc] init] autorelease]; button1_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button2_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button3_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button4_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button5_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button6_ = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1_ addTarget:self action:@selector(button1Push) forControlEvents:UIControlEventTouchUpInside]; [button2_ addTarget:self action:@selector(button2Push) forControlEvents:UIControlEventTouchUpInside]; [button3_ addTarget:self action:@selector(button3Push) forControlEvents:UIControlEventTouchUpInside]; [button4_ addTarget:self action:@selector(button4Push) forControlEvents:UIControlEventTouchUpInside]; [button5_ addTarget:self action:@selector(button5Push) forControlEvents:UIControlEventTouchUpInside]; [button6_ addTarget:self action:@selector(button6Push) forControlEvents:UIControlEventTouchUpInside]; contentView.frame = CGRectMake(0, 0, 740, 45); button1_.frame = CGRectMake(0, 2, 100, 40); button2_.frame = CGRectMake(110, 2, 100, 40); button3_.frame = CGRectMake(220, 2, 100, 40); button4_.frame = CGRectMake(330, 2, 100, 40); button5_.frame = CGRectMake(440, 2, 100, 40); button6_.frame = CGRectMake(550, 2, 100, 40); [contentView addSubview:button1_]; [contentView addSubview:button2_]; [contentView addSubview:button3_]; [contentView addSubview:button4_]; [contentView addSubview:button5_]; [contentView addSubview:button6_]; scrollView.contentSize = contentView.bounds.size; [scrollView addSubview:contentView]; [toolbar_ addSubview:scrollView]; } - (void)viewDidLoad { [super viewDidLoad]; [self toolBarMake]; } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } //ビューの一括削除 - (void)viewsClear{ [navigationController1_.view removeFromSuperview]; [navigationController2_.view removeFromSuperview]; [navigationController3_.view removeFromSuperview]; [navigationController4_.view removeFromSuperview]; [navigationController5_.view removeFromSuperview]; [navigationController6_.view removeFromSuperview]; } - (void)button1Push{ NSLog(@"button1push"); [self viewsClear]; tableViewController *tableView = [[tableViewController alloc] init]; navigationController1_ = [[UINavigationController alloc] initWithRootViewController:tableView]; navigationController1_.view.frame = CGRectMake(0, 0, 320, 416); [mainView_ addSubview:navigationController1_.view]; [self performSelector:@selector(highlightButton:) withObject:button1_ afterDelay:0.0]; } - (void)button2Push{ NSLog(@"button2push"); [self viewsClear]; tableViewController *tableView = [[tableViewController alloc] init]; tableView.tableView.backgroundColor = [UIColor blueColor]; navigationController2_ = [[UINavigationController alloc] initWithRootViewController:tableView]; navigationController2_.view.frame = CGRectMake(0, 0, 320, 416); [mainView_ addSubview:navigationController2_.view]; [self performSelector:@selector(highlightButton:) withObject:button2_ afterDelay:0.0]; } - (void)button3Push{ NSLog(@"button3push"); [self viewsClear]; tableViewController *tableView = [[tableViewController alloc] init]; tableView.tableView.backgroundColor = [UIColor lightGrayColor]; navigationController3_ = [[UINavigationController alloc] initWithRootViewController:tableView]; navigationController3_.view.frame = CGRectMake(0, 0, 320, 416); [mainView_ addSubview:navigationController3_.view]; [self performSelector:@selector(highlightButton:) withObject:button3_ afterDelay:0.0]; } - (void)button4Push{ NSLog(@"button4push"); [self viewsClear]; viewController *view = [[viewController alloc] init]; view.view.backgroundColor = [UIColor grayColor]; navigationController4_ = [[UINavigationController alloc] initWithRootViewController:view]; navigationController4_.view.frame = CGRectMake(0, 0, 320, 416); [mainView_ addSubview:navigationController4_.view]; [self performSelector:@selector(highlightButton:) withObject:button4_ afterDelay:0.0]; } - (void)button5Push{ NSLog(@"button5push"); //[self viewsClear]; viewController *view = [[viewController alloc] init]; view.view.backgroundColor = [UIColor grayColor]; view.delegate = self; navigationController5_ = [[UINavigationController alloc] initWithRootViewController:view]; navigationController5_.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:navigationController5_ animated:YES]; [self performSelector:@selector(highlightButton:) withObject:button5_ afterDelay:0.0]; } - (void)button6Push{ NSLog(@"button6push"); //[self viewsClear]; tabViewController *view = [[tabViewController alloc] init]; view.view.backgroundColor = [UIColor grayColor]; view.delegate = self; [self presentModalViewController:view animated:YES]; [self performSelector:@selector(highlightButton:) withObject:button6_ afterDelay:0.0]; } //モーダルから戻った時にどのボタンをアクティブにするのか - (void)disModalView:(NSInteger)number{ switch (number) { case 1: [self dismissModalViewControllerAnimated:YES]; [self button1Push]; break; case 2: [self dismissModalViewControllerAnimated:YES]; [self button2Push]; break; case 3: [self dismissModalViewControllerAnimated:YES]; [self button3Push]; break; case 4: [self dismissModalViewControllerAnimated:YES]; [self button4Push]; break; default: [self button1Push]; break; } } //ハイライトリセット - (void)buttonsHighlightClear{ button1_.highlighted = NO; button2_.highlighted = NO; button3_.highlighted = NO; button4_.highlighted = NO; button5_.highlighted = NO; button6_.highlighted = NO; } //ハイライトセット - (void)highlightButton:(UIButton *)button { [self buttonsHighlightClear]; button.highlighted = YES; } @end
楽はできない感じでした。
ツールバーにスクロールを設置して横に動くように作成。
ボタンを押すと、ビューにaddsubviwその前にすべてのビューを取り除く
それの繰り返し。
モーダルは消したときにどのページを表示させるかこっちで決められるようにDelegateで実装。
TabViewをモーダルで表示させてみたら、タブにはボタンを設置できないことに気が付き、タブの中のビューからデリゲートを受け取ってそれをまたデリゲートで親に引き渡すというまためんどくさいことやった。
どれにせよこうやれば出来るっぽい。
結果↓
コメントする