如何隐藏&在SplitView控制器中取消隐藏主视图控制器

时间:2011-11-05 13:02:06

标签: iphone ipad ios5 customization uisplitviewcontroller

我在XCode 4.2中创建了一个新的基于拆分视图的项目

然后在DetailViewController.m文件中添加了此方法

- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation 
{
  //This method is only available in iOS5  

   return NO;
}

现在通过这样做,我可以显示左和右我的splitview控制器的右侧部分。

现在我在我的UIBarButtonItem导航栏中添加了DetailViewController,我想通过使用哪个我可以隐藏&在Portrairt和Landscape Mode中展示我的Master View。

- (IBAction)hideUnhide:(id)sender 
{

//How can hide & unhide

}

我该怎么做?

11 个答案:

答案 0 :(得分:9)

instead spv.delegate=nil; spv.delegate=self;

你需要做下一步:

[spv willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];

答案 1 :(得分:7)

'setNeedsLayout'使UISplitViewController要求“shouldHideViewController”

- (IBAction)hideUnhide:(id)sender  {
    UISplitViewController* spv = ...;

    self.hideMaster= !self.hideMaster;
    [ spv.view setNeedsLayout ]
}

答案 2 :(得分:6)

在iOS 5.1中,您必须这样做:

内部DetailViewController.m

- (IBAction)hideUnhide:(id)sender  {
    UISplitViewController* spv = ...;

    self.hideMaster= !self.hideMaster;

    [spv.view setNeedsLayout];
    spv.delegate = nil;
    spv.delegate = self;
}

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

答案 3 :(得分:4)

我结合了以上回答,以下内容在IOS 6中运作良好:

// In split delegate
-(void)hideMaster:(BOOL)hideState
{
   _masterIsHidden = hideState;

   [self.splitViewController.view setNeedsLayout];
   self.splitViewController.delegate = nil;
   self.splitViewController.delegate = self;

   [self.splitViewController willRotateToInterfaceOrientation:[UIApplication    sharedApplication].statusBarOrientation duration:0];
}

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

答案 4 :(得分:3)

-(IBAction)clickToShowMaster:(id)sender
{
 UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
 self.navigationItem.leftBarButtonItem = systemItem1;
[self.tabBarController.tabBar setHidden:NO];
[self hideMaster:NO];
}
-(void)hideMaster:(BOOL)hideState
{

ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;

[self.splitViewController willRotateToInterfaceOrientation:[UIApplication    sharedApplication].statusBarOrientation duration:0];

 }

-(void)hideMaster:(BOOL)hideState
{
ishideMaster=hideState;
[self.splitViewController.view setNeedsLayout];
self.splitViewController.delegate = nil;
self.splitViewController.delegate = self;

[self.splitViewController willRotateToInterfaceOrientation:[UIApplication    sharedApplication].statusBarOrientation duration:0];

}


    #pragma mark - Split view

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

    if(UIInterfaceOrientationIsPortrait(orientation))
    {
        UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
        self.navigationItem.leftBarButtonItem = systemItem1;
        [self setUIforPortrait];
        return YES;
    }
     if (UIInterfaceOrientationIsLandscape(orientation))
    {
        if(ishideMaster==TRUE)
        {
            UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
            self.navigationItem.leftBarButtonItem = systemItem1;
            [self setUIForFullLandscape];
        }
        else
        {
            UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
            self.navigationItem.leftBarButtonItem = systemItem1;
            [self setUIForHalfLandscape];
        }
        return ishideMaster;
    }

}
//add the navigation button on left top, to pop-up master view.
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    [barButtonItem setImage:[UIImage imageNamed:@"down.png"]];

    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showPopup)];
    self.navigationItem.leftBarButtonItem = systemItem1;
   self.masterPopoverController = popoverController;
    self.masterPopoverController.delegate=self;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    //;
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        if(ishideMaster==FALSE)
        {
            UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"prev.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToHidemaster:)];
            self.navigationItem.leftBarButtonItem = systemItem1;
        }
        else
        {
            UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToShowMaster:)];
            self.navigationItem.leftBarButtonItem = systemItem1;

        }
    }
    else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        [self.navigationItem setLeftBarButtonItem:nil animated:YES];

    }
   //self.masterPopoverController = nil;
}

答案 5 :(得分:1)

关于wzbozon关于不需要重新分配代表的评论,我发现这两行

self.splitViewController.delegate = nil;

self.splitViewController.delegate = self;

...在模拟器上不需要,但在我的iOS5 iPad上需要1.没有它们,没有发生隐藏/显示行为(点击按钮没有折叠主视图)。

答案 6 :(得分:1)

在iOS8中,这很容易。

隐藏它

class CustomView: NSView {

@IBOutlet weak var view: NSView!
@IBOutlet weak var textField: NSTextField!

required init(coder: NSCoder) {
    super.init(coder: coder)!

    let frameworkBundle = Bundle(for: classForCoder)
    assert(frameworkBundle.loadNibNamed("CustomView", owner: self, topLevelObjects: nil))
    addSubview(view)
}

显示

[UIView animateWithDuration:0.2 animations:^{
    splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
} completion:nil];

答案 7 :(得分:0)

嗯,问题的简单部分是使用bool,比如属性hideMaster,然后

- (IBAction)hideUnhide:(id)sender 
{

   self.hideMaster= !self.hideMaster;

}

然后......

- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation 
{
  //This method is only available in iOS5  

   return self.hideMaster;
}

这样可以正常工作,但是只在重新绘制splitVC期间调用了shouldHideViewController,例如在旋转期间,所以master只隐藏/取消隐藏。

答案 8 :(得分:0)

您可以通过触发displayModeButtonItem的{​​{1}}属性的操作来显示/隐藏主ViewController:

夫特

UISplitViewController

目标-C

if let displayModeButtonItem = splitViewController?.displayModeButtonItem() {
    displayModeButtonItem.target?.performSelector(displayModeButtonItem.action)
}

对我来说,比同时干预代表,方向和布局更合适。

答案 9 :(得分:0)

SWIFT 3.0

我已经

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = self.exercises[indexPath.row] 
                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
                UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: {
                    self.splitViewController?.preferredDisplayMode = .primaryHidden
                }, completion: nil)

            }
        }
    }

答案 10 :(得分:-1)

- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
    [spv.view setNeedsLayout];  //add the line
    return self.hideMaster;
}