2012-10-22 57 views
4

我對Objective-C非常陌生,我正在嘗試爲iOS編寫我的第一個應用程序。這個想法很簡單,但我在開始架構建設時已經失敗了。Objective-C:具有分割視圖的UITabBarController

我想創建不同的視圖顯示在幾個選項卡上,這應該在視圖加載時動態創建。此外,該應用必須能夠在運行時動態添加選項卡。標籤視圖不應該在整個屏幕上運行,而應該填充頂視圖的2/3。底部剩下的1/3再次分成兩個子視圖,這些子視圖不打算用標籤開關進行更改。

我所做的是創建一個UIWindow,UITabBarController和兩個UIViewControllers(用於兩個選項卡)和一個(或兩個如圖所示),它應該在底部。

到目前爲止,我設法切換不同的標籤視圖,但只要我嘗試調整UIViewControllers的CGMakeRect爲任何大小的兩個標籤,它始終保持不變,並覆蓋整個屏幕。

在底部創建的子視圖包含一個不可點擊的按鈕。也許是因爲它是從選項卡視圖中覆蓋的。

任何人都可以給我一點幫助,我可以如何建立這些意見?

非常感謝!

這裏是我的代碼:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 


UIViewController *test = [[UIViewController alloc] init]; 
test.view.backgroundColor = [UIColor grayColor]; 
test.view.frame = CGRectMake(0, 0, 320, 200); 

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button3 setTitle:@"View 3" forState:UIControlStateNormal]; 
button3.frame = CGRectMake(30.0, 30.0, 120.0, 50.0); 
[test.view addSubview:button3]; 


UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

UIViewController *viewController1 = [[UIViewController alloc] init]; 
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1]; 
[viewController1 setTabBarItem:tab1]; 

UIViewController *viewController2 = [[UIViewController alloc] init]; 
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2]; 
[viewController2 setTabBarItem:tab2]; 


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"View from Tab 1" forState:UIControlStateNormal]; 
button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); 
[viewController1.view addSubview:button]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setTitle:@"View from Tab 2" forState:UIControlStateNormal]; 
button2.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); 
[viewController2.view addSubview:button2]; 


tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 

self.window.rootViewController = tabBarController; 
[self.window addSubview:test.view]; 

[self.window makeKeyAndVisible]; 
+0

你的岩石以編程方式創建GUI! +1。但是,在將來,請不要寫'Xcode開發'或類似的東西,因爲,爲了您的信息,Xcode對於這種情況並不是真的必要或特別相關(您可以使用nano和make來編寫應用程序)。 – 2012-10-22 22:07:11

+1

不幸的是,你可能會發現你不能做你想做的事 - 「UITabBarController」必須(根據文檔)是rootViewController(你已經完成了),但它並不擅長與其他侵犯它的草皮。你可以嘗試將標籤欄控制器嵌入到父UIViewController中的ContentView中,但是如果這是進入應用商店,我不知道Apple使用這種方式使用UITabBarController的檢查/策略。 –

+0

這是可能的,請參閱下面的答案。它可能不是最漂亮的方式,但它工作得很好。 – Tobi

回答

0

其實,有一種方法來實現這一目標得益於定製容器視圖控制器:

UIViewController自定義子類的窗口RootViewController的更換UITabBarController 。然後在viewDidLoad方法(或其他地方根據您的需要),你幾乎從上面添加幾乎你的確切代碼,稍作修改。下面是該viewDidLoad方法(我已經添加了經修改的線以上的評論)的完整代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    UIViewController *viewController1 = [[UIViewController alloc] init]; 

    //Mod1: Set an autoresizingMask 
    //so that the view always fills the tabBarController's view 
    //if needed you can also set it's frame here 
    viewController1.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1]; 
    [viewController1 setTabBarItem:tab1]; 

    UIViewController *viewController2 = [[UIViewController alloc] init]; 

    //Mod2: Same here 
    viewController2.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2]; 
    [viewController2 setTabBarItem:tab2]; 


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setTitle:@"View from Tab 1" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); 
    [viewController1.view addSubview:button]; 

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button2 setTitle:@"View from Tab 2" forState:UIControlStateNormal]; 
    button2.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); 
    [viewController2.view addSubview:button2]; 

    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; 


    //Mod3: Set the frame and autoresizingMask of the tabBarController 
    //to fill the rootVC's view 
    tabBarController.view.frame = self.view.bounds; 
    tabBarController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 


    //Mod4: This is the important part: 
    //add the tabBarController as childVC of the rootVC 
    [self addChildViewController:tabBarController]; 
    [self.view addSubview:tabBarController.view]; 
    [tabBarController didMoveToParentViewController:self]; 



    //Mod5: calculate the frame for the 'static' vc on the bottom 
    float heightForStaticVC = 200.0f; 

    float yPosForStaticVC = tabBarController.view.frame.size.height - tabBarController.tabBar.frame.size.height - heightForStaticVC; 


    UIViewController *test = [[UIViewController alloc] init]; 

    //Mod6: again setting the autoresizingMask 
    test.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; 
    test.view.backgroundColor = [UIColor grayColor]; 
    test.view.frame = CGRectMake(0, yPosForStaticVC, 320, heightForStaticVC); 

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button3 setTitle:@"View 3" forState:UIControlStateNormal]; 
    button3.frame = CGRectMake(30.0, 30.0, 120.0, 50.0); 
    [test.view addSubview:button3]; 

    //Mod7: and again adding it as childVC of the rootVC 
    [self addChildViewController:test]; 
    [self.view addSubview:test.view]; 
    [test didMoveToParentViewController:self]; 

} 

當然,只要你想,你可以修改尺寸,定位和自動尺寸調整行爲。

結果看起來是這樣的:

Tab1 PortraitTab2 Portrait

Tab1 Landscape

Tab2 Landscape

+0

非常感謝,現在看起來很棒! – user1766539