5

I have a UISplitViewController where I want to always display the master and detail view controllers for iPads. This line takes care of that for me:

// Always display master and detail in large screens
self.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

In the prepareForSegue, when opening up the detail view controllers, I have the following lines of code for the Back Button

controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true

Now when the user pressed that back button, I will want to perform an action after the Master view controller is hidden. I am not having any luck finding how to do that. In the SplitViewControllerDelegate I attempted to use:

func splitViewController(svc: UISplitViewController, willChangeToDisplayMode displayMode: UISplitViewControllerDisplayMode)

But that function is called before the Detail view controller becomes full screen. Is there a function or something else I can do to help notify me when the UISplitViewController finishes hiding the Master View?

Mike Walker
  • 2,944
  • 8
  • 30
  • 62

3 Answers3

0

You can add an observer on the view's with in the DetailViewController, and perform your specific action when it triggers:

- (void)viewDidLoad {
  [super viewDidLoad];

  [self addObserver:self forKeyPath:@"view.frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  if ([self observationInfo]) {
    @try {
      [self removeObserver:self forKeyPath:@"view.frame"];
    }
    @catch (NSException *exception) {}

  }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if (object == self && [keyPath isEqualToString:@"view.frame"]) {
    // do your action here
  }
}
Loegic
  • 3,390
  • 20
  • 33
0

You can try the delegate method

- (UISplitViewControllerDisplayMode)targetDisplayModeForActionInSplitViewController:(UISplitViewController *)svc;
daris mathew
  • 429
  • 5
  • 18
0

I got the answer from here. You need to put it in the MasterController (the one on the left side).

FYI this inactivated the expand button and to get it to expand/retract I put the extra code inside doSomethingWhenExpandButtonTapped()

navigationController?.topViewController?.navigationItem.leftBarButtonItem?.action = #selector(doSomethingWhenExpandButtonTapped)
navigationController?.topViewController?.navigationItem.leftBarButtonItem?.target = self

@objc func doSomethingWhenExpandButtonTapped() {
    print("splitViewController's expand and back button pressed")

    if splitViewController?.preferredDisplayMode == .allVisible {
        splitViewController?.preferredDisplayMode = .primaryHidden
    } else {
        splitViewController?.preferredDisplayMode = .allVisible
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256