티스토리 뷰

iOS/자료정리

[iPhone]TabbarController

썩소천사 2011. 4. 12. 18:44
반응형
//탭바를 생성 .h에 UITabBarControllerDelegate를 선언해 주어야 한다.

TestCont1 = [[TestCont1 alloc] init];
TestCont2 = [[TestCont2 alloc] init];

tabbarController = [[UITabBarController alloc]init];
tabbarController.delegate = self;
tabbarController.viewControllers = [NSArray TestCont1,TestCont2,nil];

[TestCont1 release];
[TestCont2 release];

[self.window addSubview:tabbarController.view];
[tabbarController.view release];

//탭바에 이미지를 씌운다.
tabBarImage1 = [[UIView alloc] init];
tabBarImage1.frame = CGRectMake(0, 0, 80, 50);
tabBarImage1.backgroundColor = [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"tab_theme.png"]];

[tabbarController.tabBar insertSubview:tabBarImage1 atIndex:0];

//탭바 선택시 이미지 변경
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController 
{
	NSInteger tabIndex = tabbarController.selectedIndex;
    
	tabBarImage1.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:[itemImgNameAry objectAtIndex:0]]];

	if (tabIndex == 0) {
		tabBarImage1.backgroundColor = [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"tab_theme_.png"]];
	}
}

//탭바 자체를 가리고 버튼으로 탭바를 대신 할 수 있다
//버튼을 생성한 후 tag값을 주고 버튼이 선택될 때 탭바의 SelectedIndex 에 값을 주면 된다.
[tabbarController.tabBar setHidden:YES];

UIButton *tabBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tabBtn1 setBackgroundImage:[UIImage imageNamed:@"tab_.png"] forState:UIControlStateNormal];
[tabBtn1 setBackgroundImage:[UIImage imageNamed:@"tab_1_.png"] forState:UIControlStateHighlighted];
[tabBtn1 addTarget:self action:@selector(tabBarItemSelect:) forControlEvents:UIControlEventTouchUpInside];
tabBtn1.frame = CGRectMake(0, 0, 80, 50);
tabBtn1.backgroundColor = [UIColor redColor];
[tabbarItemView addSubview:tabBtn1];
tabBtn1.tag = 0;

//버튼 선택시 호출되는 함수.
-(void)tabBarItemSelect:(UIButton *)btn
{
	[tabbarController setSelectedIndex:btn.tag];
}

반응형

'iOS > 자료정리' 카테고리의 다른 글

[iPhone]AVAudioRecorder (음성 녹음) /*.wav로 변환/  (3) 2011.05.30
[iPhone]화면 회전  (0) 2011.04.13
[iPhone][UIApplication sharedApplication]  (0) 2011.04.07
[iPhone]NSDate  (0) 2011.03.18
[iPhone]TableView 사용  (0) 2011.03.14
댓글
반응형