2010-04-23 30 views
26

是否有任何方式以編程方式隱藏splitviewcontroller中的主視圖。在我的應用程序中,第一個屏幕將是一個splitviewcontroller,我不需要爲下一個屏幕分割視圖。我可以如何實現這一點如何在UiSplitviewcontroller中隱藏iPad中的主視圖

+0

當你說隱藏masterview,你的意思是在SplitViewController的左邊創建一個空白的視圖,或者你的意思是讓DetailViewController在橫向模式下填充100%的屏幕? – 2010-04-23 17:11:32

+0

第二種選擇是讓DetailViewController以任一模式(橫向或縱向)填充屏幕的100%。 – i0707 2010-04-26 06:48:03

+0

任何解決方案呢?我正在嘗試做同樣的事情,我嘗試以模態形式呈現,如: – 2011-01-21 07:06:30

回答

5

測試馬特Gemmell創建一個名爲 「MGSplitViewController」 一個優秀的定製splitViewController。它非常容易實現,經過嚴格評論,並且包含許多優秀的功能,這些功能在普通的splitViewController中沒有找到(在橫向視圖中隱藏主視圖,在橫向視圖中更改分割位置,允許用戶在運行時更改流暢分割的大小,等等)。

信息和演示:http://mattgemmell.com/2010/08/03/mgsplitviewcontroller-updated/

直奔來源:https://github.com/mattgemmell/MGSplitViewController/

+1

用Xcode 6和iOS 8構建時要小心......看起來會炸燬構架 – whyoz 2014-11-10 19:03:28

0

雖然它不會有很好的轉換(對不起),但可以通過將根視圖設置爲詳細視圖控制器的視圖,然後使用UISplitView交換視圖並將詳細視圖移動到UISplitView。 (實際上,您可能可以爲視圖交換(推/翻頁等)設置動畫效果,但在視圖更改動畫期間更改任何內容是個不錯的主意,並且將詳細視圖移動到UISplitView的內部可能符合要求。)

0

不知道這是你想要的。舉例來說,隱藏母版視圖在橫向模式下,當按鈕被點擊,你可以做以下(在選擇方法)

UIViewController *master = [splitViewController.viewControllers objectAtIndex:0]; 
UIViewController *detail = [splitViewController.viewControllers objectAtIndex:1]; 

[master.view setFrame:CGRectMake(0, 0, 0, 0)]; 
detail.view.frame = splitViewController.view.bounds; // or use master and detail length 
+0

我試過這個,但它似乎不起作用。 – ericg 2011-03-24 21:41:33

+0

您是否看到任何尺寸更改。上面的代碼確實對我有用。嘗試手動設置detail.view.frame大小以查看大小更新。如果沒有,我會明天發佈一些細節代碼。 – surajz 2011-03-26 20:49:55

3

試試這個:

splitVC.modalPresentationStyle = UIModalPresentationFullScreen; 
[splitVC presentModalViewController:[[splitVC viewControllers] objectAtIndex:1] animated:NO]; 

在4.2對我的作品!

這是另一個可行的技巧。 video link在這裏。

+1

它肯定會隱藏主視圖,但我似乎無法在調用presentModalViewController後將其返回。 – ericg 2011-03-24 21:40:10

+0

你試過dismissModalViewController嗎? – 2011-03-25 12:17:31

+1

是的,它什麼也沒做。 – ericg 2011-03-25 15:20:06

25

在SDK 5.0中,他們爲UISplitViewControllerDelegate添加了新的方法,可以輕鬆爲您做到這一點。只是實現它像旁邊,你不會看到主視圖:

- (BOOL)splitViewController:(UISplitViewController*)svc 
    shouldHideViewController:(UIViewController *)vc 
       inOrientation:(UIInterfaceOrientation)orientation 
{ 
    return YES; 
} 

時,你可以看到它的唯一的地方是旋轉 - 母版視圖的一部分動畫過程中是可見的。我已經用簡單的方法解決了這個問題,只是在master中加載空白和黑色視圖。

PS:不確定這是否是真實的i0707。但希望這對於現在有相同問題的其他人有用。

+2

請注意,當它的'delegate'屬性發生改變時,分割視圖控制器會調用此方法來處理所有方向如果您想切換行爲,您可以強制通過調用旋轉方法,你仍然需要觸發顯示/隱藏 – 2012-03-15 00:03:41

+0

@FrankSchmitt,你能澄清一下應該設置爲nil嗎?你可以添加一些示例代碼嗎? – Shmidt 2012-04-04 11:07:01

+1

Doesn我在SVC,主人的NC,主人的VC和詳細視圖的VC中都試過了。 – Chris 2012-04-13 11:58:15

0

這工作:

附加「隱藏」的方法,以您的按鈕,例如:

UIBarButtonItem *hideButton = [[UIBarButtonItem alloc] initWithTitle:@"hide" 
             style: UIBarButtonItemStylePlain 
             target: self 
             action: @selector(hide:) 
           ]; 

[self.mainView.navigationItem setLeftBarButtonItem:hideButton]; 

在該代碼中,「self.mainView」是在導航控制器視圖控制器splitview的第二個視圖 - 僅供參考。

hide方法看起來像這樣。

-(void)hide:(id)sender 
{ 
    UIViewController *masterController = [self.viewControllers objectAtIndex:0]; 

    CGRect selfFrame = self.view.frame; 
    CGFloat aWidth = masterController.view.frame.size.width; 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.30f]; 

    if(orientation == UIDeviceOrientationLandscapeLeft) 
    { 
     selfFrame.size.height += aWidth; 
     selfFrame.origin.y -= aWidth; 
    } 
    else if(orientation == UIDeviceOrientationLandscapeRight) 
    { 
     selfFrame.size.height += aWidth; 
    } 

    [self.view setFrame:selfFrame]; 

    [UIView commitAnimations]; 

} 

這是出發點,顯然更多的邏輯需要做照顧旋轉,再次展示了它,等...

我希望這有助於。

與iOS5的和Xcode的4.3

+0

對我不起作用 – kinghomer 2012-10-19 11:34:30

3

上面我沒有工作的代碼,但是,當我試圖

CGRect selfFrame = self.splitViewController.view.frame; 

它沒有。所以....這對我的作品。(此代碼應該在你detailviewcontroller)

-(void)hideMaster { 
    UIViewController *masterController = GetAppDelegate().masterController; 

    CGRect selfFrame = self.splitViewController.view.frame; 
    CGFloat aWidth = masterController.view.frame.size.width; 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.30f]; 

    if(orientation == UIDeviceOrientationLandscapeLeft) 
    { 
     selfFrame.size.height += aWidth; 
     selfFrame.origin.y -= aWidth; 
    } 
    else if(orientation == UIDeviceOrientationLandscapeRight) 
    { 
     selfFrame.size.height += aWidth; 
    } 

    [self.splitViewController.view setFrame:selfFrame]; 

    [UIView commitAnimations]; 

    } 

爲了讓這個需要旋轉:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration]; // not sure if needed 
    //By the time this method is called the bounds of the view have been changed 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) { 
     if (masterIsHidden) { 
      [self showMaster]; 
     } 
    } else { 
     if (self.editing) { 
      [self hideMaster]; 
     } 
    } 
     [UIView setAnimationDuration:duration]; 
    [UIView commitAnimations]; 
} 
+1

對我不起作用 – kinghomer 2012-10-19 11:56:08

+0

這對我很好用。輸入editMode時,我在詳細視圖中使用它。我創建了一個showMaster方法,將主寬度保存在iVar中,然後在hideMaster中反轉+ =和 - +符號。我的實現還使用masterIsHidden iVar來防止隱藏已隱藏的視圖。 – 2014-06-04 16:36:57

5

由SplitViewController提供的BarButtonItem是關鍵以編程方式隱藏主視圖控制器。

此代碼是危險的!但優雅:)

導入目標C消息庫

#import <objc/message.h> 

下,得到一個處理當事件被觸發由SplitViewController提供

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem 
      forPopoverController:(UIPopoverController *)popoverController 
    { 
     barButtonItem.title = @"Master"; 
     [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; 

     //Obtain handle to BarButtonItem 
     [self setMasterButtonItem:barButtonItem]; 
    } 

那麼的UIBarButtonItem應該觸發自動解除主視圖控制器即

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

你可以做到這一點

objc_msgSend(self.masterButtonItem.target, self.masterButtonItem.action); 
+5

這實際上是一個很好的解決方案,與其他一些想法相比,應該是相對「安全」的。你甚至可以跳過'objc_msgSend()'並且調用'[[UIApplication sharedApplication] sendAction:masterBarButtonItem.action::masterBarButtonItem.target from:nil forEvent:nil]'。 – jrc 2013-03-15 19:09:05

7

同傑克的答案,但一個班輪。過去到 - (void)setDetailItem:(id)newDetailItem {...}來解散主。

[[UIApplication sharedApplication] sendAction: self.navigationItem.leftBarButtonItem.action 
              to: self.navigationItem.leftBarButtonItem.target 
             from: nil 
            forEvent: nil]; 
+0

我搜索了幾個小時解決這個問題,這正是我需要的。謝謝! – gplocke 2014-02-14 02:45:41

+0

在正確的時間非常有幫助。你保存了我的2小時研究:) – Nirav 2014-12-21 16:39:19

+0

這是我找到的一個特別優雅的解決方案!在我的例子中,我懶惰地實例化了一個自定義欄按鈕項,並且這個解決方案在將它與僅由UISplitViewController委託方法返回的'barButtonItem'中的動作和目標複製相結合時爲我做了竅門:-) – 2015-04-16 05:25:12

相關問題