IOS開發之三級控制器的使用與自訂標籤工具列

來源:互聯網
上載者:User

IOS開發之三級控制器的使用與自訂標籤工具列

三級控制器的概念:UITabBarController- ->(管理) UINavigationController -->(管理) UIViewController

下面筆者就分享一下三級控制器的使用

#import "MainTabbarController.h"

 

#import "ProfileViewController.h"

#import "GroupViewController.h"

#import "SearchViewController.h"

#import "CommentViewController.h"

#import "MessageViewController.h"

 

@interface UITabBarController ()

 

@end

 

@implementation UITabBarController

 

 

- (void)viewDidLoad {

 

//建立三級控制器

[self _creatView];

 

//自訂標籤工具列

[self _newInitTabbar];

 

 

}

//建立三級控制器

- (void)_creatView{

 

 

//1.建立視圖控制器並把視圖控制器添加到數組中

ProfileViewController *profileCtrl = [[[ProfileViewController alloc] init] autorelease];

 

 

GroupViewController *groupCtrl = [[[GroupViewController alloc] init] autorelease];

SearchViewController *searchCtrl = [[[SearchViewController alloc] init] autorelease];

CommentViewController *commentCtrl = [[[CommentViewController alloc] init] autorelease];

MessageViewController *messageCtrl = [[[MessageViewController alloc] init] autorelease];

//將視圖控制器存放到數組中

NSArray *viewCtrls = @[profileCtrl,groupCtrl,searchCtrl,commentCtrl,messageCtrl];

 

//2.建立導航控制器並把視圖控制器交給導航控制器管理,並且把導航控制器也添加到連一個數組中


NSMutableArray *navCtrls = [[NSMutableArray alloc] init];

for(int i=0; i<5 ; i++) {

 

//取得視圖控制器

UIViewController *viewCtrl = viewCtrls[i];

 

//建立導航控制器

UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];

[navCtrl.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_normal"] forBarMetrics:UIBarMetricsDefault];

[navCtrls addObject:navCtrl];

 

}

 

//3.建立標籤控制器,並把導航控制器交給標籤控制器管理

self.viewControllers = navCtrls;


}

//自訂標籤工具列

 

- (void)_newInitTabbar {

 

//(1)移除工具列上的按鈕

//取得tabbar上的所有子視圖

NSArray *views = [self.tabBar subviews];

for (UIView *view in views) {

[view removeFromSuperview];

}

 

//(2)設定背景

self.tabBar.backgroundImage = [UIImage imageNamed:@"navbg"];

 

//(3)建立按鈕

CGFloat width = [UIScreen mainScreen].bounds.size.width;

//每一個按鈕的寬度

CGFloat w = width/5;

for (int i=0; i<5; i++) {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

NSString *imageName = [NSString stringWithFormat:@"%d",i+1];

[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

//設定frame

button.frame = CGRectMake((w-42)/2+w*i, 2, 42, 45);

//添加一個點擊事件

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

[self.tabBar addSubview:button];

}

 

//(4)建立選中圖片

_selectedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"選中"]];

_selectedImg.frame = CGRectMake((w-53)/2.0, 2, 53, 45);

[self.tabBar addSubview:_selectedImg];

 

}

 

//按鈕的點擊事件

- (void)buttonAction:(UIButton *)button {

 

//切換視圖控制器

self.selectedIndex = button.tag;

 

//動畫

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:.2];

 

_selectedImg.center = button.center;

 

[UIView commitAnimations];

 

}



 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.