UiTabBarController這個控制器絕對是項目架構時做常用的一個控件。
我們大致看下控件的效果,我們就知道為什么說他常見了。
這就是最簡單的一個雛形,想必現在基本70%的應用界面結構都會是這樣的。
在Android中我們以ActivityGroup或是現在的fragment來實現,一個容器中包含多個子控制器。
下面我們還是以建立xib文件的形式來實現一個這樣的整體布局的例子。
當然在 xcode中我們會發現其實直接有這么一個模板了
但是直接使用模板后會發現是直接在代碼里實現了子布局得添加,由于我們不熟練,對于item,tabbar什么的圖片文字自定義,一下子找不到代碼里的api,
可能說用xib來實現可以看得比較明了。
據說以前直接有base_window模板,不過沒關系,模板只是為了給我快速建立一個應用而已,我們這邊手動的從最基礎得開始
1.首先建立一個Empty Application
2.建立完后我們自定頂一個MainWindow.xib(當然,這個名字可以隨意取,但是按照規范以及一種默認留下來的習慣),作為應用啟動時加載的首個nib文件,
在新建xib文件你可以選擇window也可以是empt什么,其實都差不多,我們這邊選window模板得xib文件
3.然后我們其實是要把XXXAppdelegate和這個xib文件連接起來。因此把.h文件定義成這樣:
@interface NonoAppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
UITabBarController *tabTarController;
}
@property (retain, nonatomic) IBOutlet UIWindow *window;//該控件模板生成是不帶IBOutlet的,但是我們為了xib文件布局得統一性,將其也作為一個輸出口和在Mainxib中連接起來可以
@property (retain, nonatomic) IBOutlet UITabBarController *tabTarController;
@end
5.好了,那么我們在object下的添加一個delegate的對象,操作很簡單,在右側控件組中
拖一個這樣得對象到xib下得Object標簽下,
然后我們來自定這個對象,根據上面所知,我們大致可以知道我們需要一個類似于delegate類得對象,對了
我們的AppDelegate不就剛好是這么一個東西么。于是很自然的,選中這個object然后在右側屬性欄將custom class設置成NonoAppDelagate。
然后點擊File's Owner將其輸出口delegate和我們剛放上去的NonoAppdelagate鏈接起來。
6.設置完以上后,我們可以點擊Object下得XXXAppDelegate,然后看右邊屬性欄的 Outlets,對了,我們剛在該文件得.h中申明過兩個輸出口,此刻我們是
要創建兩個這樣的對象然后將其連接起來。window這時已經有了,還少個UITabBarController。那么我們從右邊拖個過來咯
然后輸出口和對象鏈接起來。
此刻,最基本的tabbarcontroller布局框架就ok了,此刻的xib文件如下圖
然后我們打開AppDelegate.m進行實現和修改
@implementation NonoAppDelegate
@synthesize window ;
@synthesize tabTarController ;
- (void)dealloc
{
[self.window release];
[self.tabTarController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.tabTarController;
[self.window makeKeyAndVisible];
return YES;
}
這個具體你可以去看上一篇中提到過ios應用啟動內容,有個不錯得博文給了很好得一個解釋。
此刻我們可以將main.h代碼稍微修改下
#import "NonoAppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, nil);
}
}
7.好了。最基本得tabbar界面完成,接下來是往里面添加子控制器。
首先我們來熟悉下UITabBarController這個控件:
從界面我們可以推測出,該控件里面大致上有什么東西:
1>.應該有個類似于管理一組子控件的東西吧。
2>界面下面切換得切換條吧 tabBar
3>當然還回發現有個delegate這樣得東西,就是代理對象么。
當然上面是在代碼中,我們得到這些屬性,做相應得操作。
在xib文件里可能說看上去會比較直觀
1>Tab bar里面放的是由一個或是多個TabbarItem組成的數組,每個itm對應一個ViewController。
2>下面的 First,Second等等就是每個Item對應 的Controller,這里也要注意一點,默認的我們我們拖進去一個
TabBarItem,一般我們會設置對應得XXXcontroller.xib文件
之后還需將Custom class改成對應XXXXController類,因為默認的類是UIViewController。這會在應用啟動后報錯的。
3>tabbarItem中可以設置title ,系統默認的幾種圖標,還有是自定義圖標,以及badgeValue,就是上面標簽上每個紅色的值,
這個比Android上先見之明多了呵呵。
4> 當底部的按鈕超過5個時,系統會自動增加一個more按鈕,點擊more后,剩余的按鈕會被顯示出來。
8.UITabbarController左右滑動切換標簽頁
每個Tabbar ViewController都要添加如下代碼,建議在基類中添加:
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
再添加2個函數,包含切換動畫效果:
{
NSUInteger selectedIndex = [self.tabBarController selectedIndex];
NSArray *aryViewController = self.tabBarController.viewControllers;
if (selectedIndex < aryViewController.count - 1) {
UIView *fromView = [self.tabBarController.selectedViewController view];
UIView *toView = [[self.tabBarController.viewControllers objectAtIndex:selectedIndex + 1] view];
[UIView transitionFromView:fromView toView:toView duration:0.5f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
if (finished) {
[self.tabBarController setSelectedIndex:selectedIndex + 1];
}
}];
}
}
{
NSUInteger selectedIndex = [self.tabBarController selectedIndex];
if (selectedIndex > 0) {
UIView *fromView = [self.tabBarController.selectedViewController view];
UIView *toView = [[self.tabBarController.viewControllers objectAtIndex:selectedIndex - 1] view];
[UIView transitionFromView:fromView toView:toView duration:0.5f options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
if (finished) {
[self.tabBarController setSelectedIndex:selectedIndex - 1];
}
}];
}
}
新聞熱點
疑難解答