開發(fā)app時經(jīng)常用到的一個基本布局框架就是tabbar+navigation,也就是下方tabbar分模塊,然后在各個模塊中使用導(dǎo)航欄深入到各個界面。
成都創(chuàng)新互聯(lián)公司是一家以重慶網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計、品牌設(shè)計、軟件運(yùn)維、seo優(yōu)化排名、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為成都食品包裝袋等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
有時候我們需要在跳轉(zhuǎn)到某些界面時隱藏下方的tabbar,怎么做呢,很簡單:
這樣就可以了,其實最后一行不要也能行。
只要在對某個界面設(shè)置隱藏后,從這個界面繼續(xù)深入往下的界面也是會自動隱藏的,不需要對每個都設(shè)置。
這里的bottombar不單單作用于tabbar,其他的bar也是可以的。但一般常用的還是tabbar。
查看作者首頁
UITabBarController是IOS中很常用的一個viewController,例如系統(tǒng)的鬧鐘程序,ipod程序等。UITabBarController通常作為整個程序的rootViewController,而且不能添加到別的container viewController中。
首先我們看一下它的view層級圖:
一、手動創(chuàng)建UITabBarController
最常見的創(chuàng)建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因為UITabBarController通常是作為整個程序的rootViewController的,我們需要在程序的window顯示之前就創(chuàng)建好它,具體步驟如下:
1、創(chuàng)建一個UITabBarController對象
2、創(chuàng)建tabbarcontroller中每一個tab對應(yīng)的要顯示的對象
3、通過UITabBarController的viewController屬性將要顯示的所有content viewcontroller添加到UITabBarController中
4、通過設(shè)置UITabBarController對象為window.rootViewController,然后顯示window
下面看一個簡單的例子:
復(fù)制代碼
復(fù)制代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
SvTabBarFirstViewController *viewController1, *viewController2;
viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController1.title = @"First";
viewController2 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];
viewController2.title = @"Second";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[viewController1 release];
[viewController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
復(fù)制代碼
復(fù)制代碼
二、UITabBarItem
UITabBar上面顯示的每一個Tab都對應(yīng)著一個ViewController,我們可以通過設(shè)置viewcontroller.tabBarItem屬性來改變tabbar上對應(yīng)的tab顯示內(nèi)容。否則系統(tǒng)將會根據(jù)viewController的title自動創(chuàng)建一個,該tabBarItem只顯示文字,沒有圖像。當(dāng)我們自己創(chuàng)建UITabBarItem的時候,我們可以顯示的指定顯示的圖像和對應(yīng)的文字描述。當(dāng)然還可以通過setFinishedSelectedImage:withFinishedUnselectedImage:方法給選中狀態(tài)和飛選中狀態(tài)指定不同的圖片。下面看個自己創(chuàng)建UITabBarItem的小例子:
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]
withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
viewController2.tabBarItem = item;
[item release];
此外UITabBarItem還有一個屬性badgeValue,通過設(shè)置該屬性可以在其右上角顯示一個小的角標(biāo),通常用于提示用戶有新的消息,使用很簡單,后面有例子。
三、moreNavigationController
UITabBar上最多可以顯示5個Tab,當(dāng)我們往UITabBarController中添加超過的viewController超過5個時候,最后一個一個就會自動變成,按照設(shè)置的viewControlles的順序,顯示前四個viewController的tabBarItem,后面的tabBarItem將不再顯示。當(dāng)點擊more時候?qū)棾鲆粋€標(biāo)準(zhǔn)的navigationViewController,里面放有其它未顯示的的viewController,并且?guī)в幸粋€edit按鈕,通過點擊該按鈕可以進(jìn)入類似與ipod程序中設(shè)置tabBar的編輯界面。編輯界面中默認(rèn)所有的viewController都是可以編輯的,我們可以通過設(shè)置UITabBarController的customizableViewControllers屬性來指定viewControllers的一個子集,即只允許一部分viewController是可以放到tabBar中顯示的。但是這塊兒要注意一個問題就是每當(dāng)UITabBarController的viewControllers屬性發(fā)生變化的時候,customizableViewControllers就會自動設(shè)置成跟viewControllers一致,即默認(rèn)的所有的viewController都是可以編輯的,如果我們要始終限制只是某一部分可編輯的話,記得在每次viewControlles發(fā)生改變的時候,重新設(shè)置一次customizableViewControllers。
四、UITabBarController的Rotation
UITabBarController默認(rèn)只支持豎屏,當(dāng)設(shè)備方向放生變化時候,它會查詢viewControllers中包含的所有ViewController,僅當(dāng)所有的viewController都支持該方向時,UITabBarController才會發(fā)生旋轉(zhuǎn),否則默認(rèn)的豎向。
此處需要注意當(dāng)UITabBarController支持旋轉(zhuǎn),而且發(fā)生旋轉(zhuǎn)的時候,只有當(dāng)前顯示的viewController會接收到旋轉(zhuǎn)的消息。
五、UITabBar
UITabBar自己有一些方法是可以改變自身狀態(tài),但是對于UITabBarController自帶的tabBar,我們不能直接去修改其狀態(tài)。任何直接修改tabBar的操作將會拋出異常,下面看一個拋出異常的小例子:
復(fù)制代碼
復(fù)制代碼
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
self.tabBarController.tabBar.selectedItem = nil;
復(fù)制代碼
復(fù)制代碼
上面代碼的最后一行直接修改了tabBar的狀態(tài),運(yùn)行程序回得到如下結(jié)果:
六、Change Selected Viewcontroller
改變UITabBarController中當(dāng)前顯示的viewController,可以通過一下兩種方法:
1、selectedIndex屬性
通過該屬性可以獲得當(dāng)前選中的viewController,設(shè)置該屬性,可以顯示viewControllers中對應(yīng)的index的viewController。如果當(dāng)前選中的是MoreViewController的話,該屬性獲取出來的值是NSNotFound,而且通過該屬性也不能設(shè)置選中MoreViewController。設(shè)置index超出viewControllers的范圍,將會被忽略。
2、selectedViewController屬性
通過該屬性可以獲取到當(dāng)前顯示的viewController,通過設(shè)置該屬性可以設(shè)置當(dāng)前選中的viewController,同時更新selectedIndex。可以通過給該屬性賦值
tabBarController.moreNavigationController可以選中moreViewController。
3、viewControllers屬性
設(shè)置viewControllers屬性也會影響當(dāng)前選中的viewController,設(shè)置該屬性時UITabBarController首先會清空所有舊的viewController,然后部署新的viewController,接著嘗試重新選中上一次顯示的viewController,如果該viewController已經(jīng)不存在的話,會接著嘗試選中index和selectedIndex相同的viewController,如果該index無效的話,則默認(rèn)選中第一個viewController。
七、UITabBarControllerDelegate
通過代理我們可以監(jiān)測UITabBarController的當(dāng)前選中viewController的變化,以及moreViewController中對編輯所有viewController的編輯。通過實現(xiàn)下面方法:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
該方法用于控制TabBarItem能不能選中,返回NO,將禁止用戶點擊某一個TabBarItem被選中。但是程序內(nèi)部還是可以通過直接setSelectedIndex選中該TabBarItem。
下面這三個方法主要用于監(jiān)測對moreViewController中對view controller的edit操作
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
修改?TabBar?的?subview?的?frame?就可以了。其中,TabBar?的subview?共有兩個,一個叫UITabBar,另一個叫UITranstionview。將UITabBar的?y?向下移49個單位,把UITranstionview 的?hight?加長 49 個單位。但是有時候這樣隱藏tabbar后,原來的viewcontroller上的view就會被拉長。解決方法是修改viewcontroller上的view?的autosizing。
以下是具體代碼,另加上了動畫效果:
for?(UIView?*v?in?[self.tabBarController.view?subviews])?{
if?([v?isKindOfClass:[UITabBar?class]])?{
[UIView?animateWithDuration:kDuration?delay:0.0f?options:UIViewAnimationOptionCurveEaseOut?animations:^(){
CGRect?frame?=?v.frame;
frame.origin.y?+=?49.0f;
v.frame?=?frame;
}?completion:^(BOOL?complete)
{
isAnimating?=?NO;
}];
}?else?{
isAnimating?=?YES;
[UIView?animateWithDuration:kDuration?delay:0.0f?options:UIViewAnimationOptionCurveEaseOut?animations:^(){
CGRect?frame?=?v.frame;
frame.size.height?+=?49.0f;
v.frame?=?frame;
}?completion:nil];
}
}
如果使用系統(tǒng)的tabBar 最多只能顯示5個,所以想要展示超過5個 就只能自己想辦法弄嘍!我用的辦法是:
1:先把系統(tǒng)的tabBar 隱藏掉 ?[ self .tabBar setHidden: YES ]; ? ps:在繼承了UITabBarController 的tabBar控制器里面!?
2:自定義個view添加到自定義的tabBar控制器里面,上面創(chuàng)建你想要的個數(shù)的按鈕!然后放在tabBar的位置上!
3:按鈕點擊的時候切換 tabBar 控制器的 selectIndex !至于按鈕的圖片文字之類的 使用按鈕的系統(tǒng)類型UIButtonTypeCustom 設(shè)置普通和選中的圖片以及文字 文字顏色等!
如圖:
網(wǎng)站欄目:ios開發(fā)tabbar,IOS開發(fā)者模式
當(dāng)前URL:http://jinyejixie.com/article6/dsedcog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、微信小程序、網(wǎng)站營銷、商城網(wǎng)站、網(wǎng)站策劃、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)