国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)中UITabBarController的使用示例

2019-10-21 18:57:16
字體:
供稿:網(wǎng)友
這篇文章主要介紹了iOS開發(fā)中UITabBarController的使用示例,代碼基于Objective-C進(jìn)行演示,需要的朋友可以參考下
 

首先我們看一下它的view層級圖:

復(fù)制代碼代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    // Override point for customization after application launch.  
    self.window.backgroundColor = [UIColor whiteColor];  
   
#pragma mark - 設(shè)置tabBarItem  
#pragma mark  第一個(gè)視圖ViewController  
      
    HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];  
    // 設(shè)置A視圖下----標(biāo)簽欄標(biāo)題文字(可參照微信或者QQ體會)  
    tabBarViewA.tabBarItem.title = @"微信";  
    // 設(shè)置A視圖下----標(biāo)簽欄圖片(因?yàn)樽约簺]有圖片,在這里隨便設(shè)置了個(gè)名字)  
    //tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];  
    // 設(shè)置A視圖下----標(biāo)簽欄信息提示(住:badgeValue是NSString類型 如下設(shè)置了3,就像QQ消息有3條未接受一樣,給人一種提醒)  
    tabBarViewA.tabBarItem.badgeValue = @"3";  
    // ios7棄用了----標(biāo)簽欄選中的時(shí)候顯示一張圖片,沒選中的時(shí)候顯示另一張圖片  
    //[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];  
    // ios7的方法(自己沒有圖片,所以代碼里面的圖片都是一個(gè)隨便取的名字,沒有實(shí)用意義)  
    //tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;  
      
#pragma mark  第二個(gè)視圖ViewController  
    // 第二個(gè)視圖ViewController  
    HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];  
    // 設(shè)置B視圖下----標(biāo)簽欄  
    // 用系統(tǒng)提供的標(biāo)識(可以算等價(jià)于圖標(biāo)和文字)進(jìn)行設(shè)置(參數(shù):UITabBarSystemItem是個(gè)枚舉值,想要什么形式,就去系統(tǒng)提供的API中找)  
    tabBarViewB.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];  
    // 設(shè)置B視圖下----標(biāo)簽欄信息提示  
    tabBarViewB.tabBarItem.badgeValue = @"GO";  
      
#pragma mark  第三個(gè)視圖ViewController  
    HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];  
    tabBarViewC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];  
    // 設(shè)置B視圖下----標(biāo)簽欄信息提示  
    tabBarViewC.tabBarItem.badgeValue = @"new";  
      
#pragma mark  第四個(gè)視圖ViewController  
    HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];  
    tabBarViewD.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];  
    // 設(shè)置B視圖下----標(biāo)簽欄信息提示  
    tabBarViewD.tabBarItem.badgeValue = @"99";  
      
#pragma mark  第五個(gè)視圖ViewController  
    HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];  
    tabBarViewE.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];  
    // 設(shè)置B視圖下----標(biāo)簽欄信息提示  
    tabBarViewE.tabBarItem.badgeValue = @"sky";  
      
#pragma mark  第六個(gè)視圖ViewController(系統(tǒng)默認(rèn)能顯示的最大視圖個(gè)數(shù)是5個(gè))  
    /* 如果你的viewControllers屬性添加了多于五個(gè)的items,那tab bar controller將會自動插入一個(gè)特殊的view controller, 
    稱為 More view controller,該 controller 將會負(fù)責(zé)管理多于的items,這個(gè)More view controller提供一個(gè)自定義的界面, 
    用table的方式呈現(xiàn)多余的view controller,并且view controller的數(shù)量是不限制的*/  
    HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];  
    tabBarViewF.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];  
    // 設(shè)置F視圖下----標(biāo)簽欄信息提示  
    tabBarViewF.tabBarItem.badgeValue = @"AG";  
      
      
      
#pragma mark - 設(shè)置TabBarController  
      
    // 創(chuàng)建TabBarController  
    UITabBarController * tabBarController = [[UITabBarController alloc]init];  
    // TabBarController默認(rèn)是放在最底部的,如果你想調(diào)整位置,可以進(jìn)行下面2部操作(44是iPhone中TabBarController和UINavigationController標(biāo)準(zhǔn)高度)  
    //CGRect frame = CGRectMake(0, 20, 320, 44);  
    //tabBarController.tabBar.frame = frame;  
    // 每一個(gè)tab都必須有一個(gè)content view controller------->viewControllers屬性,用來存入一個(gè)應(yīng)用的TabBarController有多少個(gè)界面切換  
    tabBarController.viewControllers = [NSArray arrayWithObjects:tabBarViewA,tabBarViewB,tabBarViewC,tabBarViewD,tabBarViewE,tabBarViewF, nil nil];  
    // 設(shè)置著色  
    tabBarController.tabBar.tintColor = [UIColor greenColor];  
    // 設(shè)置選中圖片時(shí)候  
    tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];  
    // 設(shè)置背景圖片(自己沒有圖片,不進(jìn)行設(shè)置)  
    //tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];  
    // 設(shè)置程序啟動時(shí)默認(rèn)的ViewController視圖(設(shè)置為3,一共5個(gè)ViewController,進(jìn)來時(shí)候顯示的視圖就是第4個(gè)-tabBarViewD,下標(biāo)從0開始)  
    tabBarController.selectedIndex = 3;  
      
      
    self.window.rootViewController = tabBarController;  
      
    [self.window makeKeyAndVisible];  
    return YES;  
}  

 

最后效果如下圖:

iOS開發(fā)中UITabBarController的使用示例

UITabBarController的代理方法以及模態(tài)顯示
首先要實(shí)現(xiàn)協(xié)議<UITabBarControllerDelegate>

復(fù)制代碼代碼如下:
    // 設(shè)置代理
    tabBarController.delegate =self;  
    //UINavigationController *nav = tabBarController.moreNavigationController;
    //[nav setNavigationBarHidden:YES animated:YES];

 

// 控制哪些ViewController的標(biāo)簽欄能被點(diǎn)擊
- (BOOL)tabBarController:(UITabBarController *)tabBarControllershouldSelectViewController:(UIViewController *)viewController{
    // 代表HMT_CViewController這個(gè)View無法顯示,無法點(diǎn)擊到它代表的標(biāo)簽欄
    if ([viewControllerisKindOfClass:[HMT_CViewControllerclass]]) {
        returnNO;
    }
    returnYES;
}

// 選中哪個(gè)標(biāo)簽欄,一個(gè)監(jiān)控作用吧
- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController{

}

// More view controller將要開始編輯
- (void)tabBarController:(UITabBarController *)tabBarControllerwillBeginCustomizingViewControllers:(NSArray *)viewControllers{

}
// More view controller將要結(jié)束編輯
- (void)tabBarController:(UITabBarController *)tabBarControllerwillEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{

}
// More view controller編輯
- (void)tabBarController:(UITabBarController *)tabBarControllerdidEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{

}

#import "HMT-AViewController.h"
#import "HMTModalShowViewController.h"

@interfaceHMT_AViewController ()
@end

@implementation HMT_AViewController

- (void)viewDidLoad
{
    [superviewDidLoad];
    self.view.backgroundColor = [UIColorredColor];
    
    // 創(chuàng)建一個(gè)按鈕
    UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame =CGRectMake(100,100,100, 100);
    [button addTarget:self action:@selector(modalShow)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
// Do any additional setup after loading the view.
}

- (void)modalShow{
    
    HMTModalShowViewController * modalShowVC = [[HMTModalShowViewController alloc]init];

    //模態(tài)視圖控制器呈現(xiàn)出來時(shí)候的視覺效果
    modalShowVC.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;
    /*
     UIModalTransitionStyleCoverVertical = 0,   //默認(rèn),由下往上
     UIModalTransitionStyleFlipHorizontal,      //水平轉(zhuǎn)動效果
     UIModalTransitionStyleCrossDissolve,       //漸變效果
     UIModalTransitionStylePartialCurl,         //書頁往上翻動效果
     */
    //模態(tài)視圖控制器呈現(xiàn)方式,默認(rèn)全屏
    modalShowVC.modalPresentationStyle =UIModalPresentationFullScreen;
    /*    
     UIModalPresentationFullScreen = 0,
     UIModalPresentationPageSheet,
     UIModalPresentationFormSheet,
     UIModalPresentationCurrentContext,
     UIModalPresentationCustom,
     UIModalPresentationNone = -1,    
     */
    
    UINavigationController * modalShowNC = [[UINavigationController alloc] initWithRootViewController:modalShowVC];
    
    //推出模態(tài)視圖控制器
    [self presentViewController:modalShowNC animated:YES completion:^{
        NSLog(@"hello world");       
    }];
}


#import "HMTModalShowViewController.h"

@interfaceHMTModalShowViewController ()

@end

@implementation HMTModalShowViewController

- (void)viewDidLoad
{
    [superviewDidLoad];
// Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    // 利用UINavigationController來實(shí)現(xiàn)退出控制器
    UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(modalDismiss)];
    
    self.navigationItem.leftBarButtonItem = barButton;
    self.navigationItem.title =@"humingtao";
    
    //創(chuàng)建一個(gè)按鈕來實(shí)現(xiàn)退出控制器
/*    UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(modalDismiss) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];*/
    
}

- (void)modalDismiss{
   //退出模態(tài)視圖控制器
    [self dismissViewControllerAnimated:YES completion:^{ 
        NSLog(@"退出GoodBye");
    }];
}
@end

 


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 萍乡市| 楚雄市| 阳朔县| 胶州市| 青龙| 拉孜县| 新兴县| 陕西省| 竹溪县| 隆德县| 芦溪县| 剑阁县| 长宁县| 石渠县| 同仁县| 抚宁县| 望城县| 宜城市| 江永县| 乐安县| 松阳县| 中西区| 察哈| 南丹县| 德江县| 鱼台县| 科技| 海门市| 舟山市| 凌源市| 罗田县| 东海县| 宜兰市| 东光县| 龙岩市| 永丰县| 连山| 电白县| 壤塘县| 太康县| 常山县|