簡體   English   中英

如何用UITabBarController打開一個模態並在關閉時顯示最后一個標簽欄視圖?

[英]How to open a modal with UITabBarController and on close, display last tab bar view?

所以我在一個故事板上有一個UITabBarController ,它有3個按鈕。 其中兩個通過標准視圖關聯segues打開與標簽欄相關聯的標准視圖......第三個項目是問題。

我想在模態視圖中打開一個存在於另一個故事板(由應用程序中的其他視圖共享)的表單,並在關閉或提交時,返回到之前在選項卡式視圖中處於活動狀態的任何選項卡。

我已經看到了一些“選項”,但它們似乎都不像我想做的那樣。

有任何想法嗎?

編輯:它是由我的原始問題編輯的mod,但我在Swift中寫這個...不是Obj-C。

所以我有一個潛在的解決方案,使用空白視圖控制器,shouldSelectViewController和UITabBarController委托。 唯一的問題是,我對它的脆弱程度並不特別滿意。

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

    // This is a bit dangerous if we move where 'newController' is located in the tabs, this will break.
    let newController = viewControllers![1] as! UIViewController

    // Check if the view about to load is the second tab and if it is, load the modal form instead.
    if viewController == newController {
        let storyboard = UIStoryboard(name: "ModalController", bundle: nil)
        let vc = storyboard.instantiateInitialViewController() as? UIViewController

        presentViewController(vc!, animated: true, completion: nil)

        return false
    } else {
        return true
    }
}

這樣可以正常工作,但是如果有人出現並重新排列故事板中選項卡的順序,這將會破壞。

另外,為了給@rmp一些功勞,另外一個選項是“如果你為tabBarController中的所有根視圖控制器提供一個標題,你可以使用viewController.title來檢查在shouldSelectViewController委托中點擊了哪個標簽。當然,您仍然冒着有人重命名視圖標題屬性的風險。“

我已模擬相同,請參考鏈接: - https://github.com/deepesh259nitk/MultipleStoryBoards/tree/master

  1. 運行該項目
  2. 點擊Tab2
  3. 你會看到3個按鈕
  4. 點擊按鈕3

聽起來你真的不想要一個標簽,標簽意味着包含一個視圖而不是一個新的/模態視圖。 但是,有些情況下你想要做的事情是有意義的,就像一些應用程序會使用中心選項卡作為按鈕來添加新項目或執行一些操作(Yelp就是一個很好的例子)。 在這些情況下,標簽通常較大或視覺上不同,表示它實際上不是標簽。 這就是我認為你真正追求的。

要做到這一點,你需要:

  1. tabBar創建一個選項卡,這將只是一個占位符
  2. 創建一個圖標或圖像,用作將覆蓋占位符選項卡的按鈕
  3. 使用viewWillLayoutSubviews您的tabBarController類將按鈕放置占位符標簽。 見下文(肖像示例代碼)。
  4. 向將顯示模態視圖的按鈕添加操作。

注意:您可能需要使用下面的代碼調整按鈕的位置以滿足您的需求。

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];

            if (UIDeviceOrientationIsPortrait && !centerTabButton) {

                //CUSTOM CENTER TAB BAR BUTTON
                UIImage *buttonImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];
                UIImage *highlightImage = [UIImage imageNamed:@"icn_CenterTabBarIcon.png"];

                centerTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
                centerTabButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
                [centerTabButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
                [centerTabButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
                [centerTabButton setShowsTouchWhenHighlighted:FALSE];
                [centerTabButton setAdjustsImageWhenHighlighted:FALSE];
                [centerTabButton addTarget:self action:@selector(centerTabClicked:) forControlEvents:UIControlEventTouchUpInside];

                CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;

                //center the button on the tab bar based on the tab bar's height
                if (heightDifference < 0){
                    centerTabButton.center = self.tabBar.center;
                }
                else{
                    CGPoint center = self.tabBar.center;
                    center.y = center.y - heightDifference/2.0;
                    centerTabButton.center = center;
                }

                [self.view addSubview:centerTabButton];
            }
        }

回答你關於它“脆弱”的第二個問題

如果您為您的所有根viewControllers的是彌補你的選項卡提供了一個標題tabBarController比你可以使用viewController.title檢查其標簽是在抽頭shouldSelectViewController委托。 當然,您仍然冒着有人重命名視圖標題屬性的風險。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM