2016年6月1日 星期三

tabBarControllerWith -----NIB



目前知道三種方法,先備知識,請看如何使用xib,

第一種:

網址:

https://github.com/web8246/TabBarWithXIB.git

1.製造出需要的ViewController.....(多個)

2.設置圖片和title等

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:@"ViewController1" bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Red";
        UIImage *image = [UIImage imageNamed:@"a.jpg"];
        CGImageRef imageRef = image.CGImage;
        self.tabBarItem.image = [[UIImage imageWithCGImage:imageRef scale:2 orientation:
                                  UIImageOrientationDown] imageWithRenderingMode:
                                 UIImageRenderingModeAlwaysOriginal];
    }
    return self;

}



3.appDelegate設置:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    
    
//    self.viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
//    self.window.rootViewController = self.viewController;
    
    
    UITabBarController *tabController = [[UITabBarController alloc] init];
    ViewController1 *vc1 = [ViewController1 new];
    ViewController2 *vc2 = [ViewController2 new];
    ViewController3 *vc3 = [ViewController3 new];
    [tabController setViewControllers:[NSArray arrayWithObjects:vc1,vc2,vc3, nil] animated:YES];
    
    self.window.rootViewController = tabController;
    
    
    
    
    
    [self.window makeKeyAndVisible];
    return YES;




上面這種方式,使用簡單,我想,唯一的缺點就是,對於tabbar那一條,在沒有實際build之前,都無法確定樣式


下面這一種,我個人覺得,雖然說,可以看到tabbar那一條實際畫面,但是,實在不是很方便,而且,插入的view一整個就是有鬼打牆的感覺,畫面反而變得不好調適


第二種:

重點:一樣一個rootcontroller檔案,重點是在xib上面,直接丟上去一個tabbar,

這樣就可以直接在畫面上看到tabbar,

之後設定tabbardelegate


然後要跳轉的地方:

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//    Vc2 *vc2 = [[Vc2 alloc] initWithNibName:nil bundle:nil];
    NSLog(@"%@",item.title);
    if([item.title isEqualToString:@"abc"]) {
        // do something for this specific button
        
        [self.view insertSubview:vc2.view belowSubview:self.myTabBar];
        
    }
    if ([item.title isEqualToString:@"ddd"]) {
        [vc2.view removeFromSuperview];//這樣才會去掉view
        [self.presentingViewController.view removeFromSuperview];//這樣沒換掉view,只去掉view
    }

}



最後:

https://github.com/web8246/TestTabBarXib.git


第三種:

嘗試上面兩種我就一直想,希望可以把tabbar給輕鬆的看的到,當然,如果無法輕鬆看到,至少也要讓我可以簡單的設置它,另外就是我希望可以簡單的讓他達成跳轉頁面(不是tabbar內跳轉)

於是就有了第三種方式:

重點是:

tabbarcontroller要繼承自uitabbarcontroller,

要跳轉過去的方法內,才去設定tabbarcontroller的viewcontrollers(不要在tabbarcontroller裡面的viewdidload去設定)

其他就看code吧xd~

網址:

https://github.com/web8246/TestTableViewCellWithXIB-2.git


沒有留言:

張貼留言