2011-07-25 154 views
7

我嘗試了下面的代碼,試圖讓自定義視圖顯示在標籤欄控制器上方(恰好在其所有選項卡中都有一個導航控制器)。在標籤欄控制器/導航控制器上添加自定義視圖?

問題是它重疊在導航欄的頂部,我想導航欄向下移動。

我試着設置標籤欄控制器的框架,但是根本沒有移動它。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Override point for customization after application launch. 
    // Add the tab bar controller's current view as a subview of the window 
    //self.tabBarController.view.frame = CGRectMake(0, 62, 320, 320); 
    self.window.rootViewController = self.tabBarController; 

    [self.window makeKeyAndVisible]; 

    // setting up the header view 
    self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 20, 320, 42)]; 
    [self.window addSubview:self.headerView]; 

    // setting up facebook stuff 
    AgentSingleton *agentSingleton = [AgentSingleton sharedSingleton]; 
    agentSingleton.facebook = [[Facebook alloc] initWithAppId:APP_ID]; 

    return YES; 
} 

任何想法?

回答

7

沒有什麼好的方法可以做到這一點。 UINavigationController和UITabBarController的各種子視圖都是私有的,試圖混淆它們可能無法正常工作。 Apple並沒有給我們提供創建「容器」視圖控制器的工具,所以你不能輕易地將UINavigationController/UITabBarController嵌入到另一個視圖控制器中,或者自己重新創建UINavigationController/UITabBarController。

你最好打賭可能會繼續嘗試創建你自己的「容器」視圖控制器,並處理一些不正確的事情。尤其是,包含的視圖控制器的parentViewController將返回零,因此包含的視圖控制器或其子控制器上的各種其他事物將被破壞(例如interfaceOrientation屬性將錯誤,presentModalViewController:animated:可能無法正確工作)。其他的事情也可能被打破。

或者您可以等到iOS的某些未來版本實際上支持我們創建容器視圖控制器(如果曾經),然後僅支持該版本。

0

你的意思是上面的垂直高於其他內容?

或以上,因爲坐在其他內容的頂部?

有presentModalViewController:動畫:但我懷疑這就是你想要的?

+0

垂直高於其餘內容。 – xil3

+0

我把它放在頂部,但目前它覆蓋了導航控制器。我需要按下標籤欄控制器...... – xil3

+0

使用一個modalViewController,其上的背景視圖是當前視圖控制器上的最頂層視圖(類似於view.superview.superview.superview,直到它爲零) – dwbrito

7

你可以簡單addSubview到窗口:

[self.view.window addSubview:myView]; 
+0

對我來說,自我.view.window爲null,因此添加子視圖不起作用。 – Lewis42

+0

@ Lewis42並不是說這是正確的方法,而是在viewDidLoad方法窗口屬性內部始終爲零。這需要在viewDidAppear方法內完成。 –

0

我不能評論,所以我會在這裏寫這篇文章的其他人誰碰到這個頁面出現。被投票的Borut的答案實際上是更容易和更正確的答案。

他根本沒有提供給你正確的方向。您需要將自定義視圖添加到您的AppDelegate中聲明的應用程序的窗口中(在Mono中,這將是AppDelegate.Window.Add(myView),而不是,該視圖的窗口爲null(例如this.View .Window)。

這個工作在Xamarin /單聲道不錯,我不明白爲什麼它不會對對象 - 同在正確的代碼。

20

添加視圖到tabBarController視圖本身:

[self.tabBarController.view addSubview:yourView]; 
0

你可以做這樣的事情:

if let indeedTabBarController = self.tabBarController { 

    let buttonHeight: CGFloat = 49 // height of tab bar 
    let buttonWidth = UIScreen.main.bounds.width/3 // in case if you have 3 tabs 
    let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight) 
    let button = UIButton(frame: frame) 
    button.addTarget(self, action: #selector(self.someAction), for: .touchUpInside) 

    indeedTabBarController.view.addSubview(button) 
} 
相關問題