Teja Nandamuri
Teja Nandamuri

Reputation: 11201

viewWillAppear is not called in the master view of UISplitViewController

I have a splitvew controller (set in storyboard) with master and detail, and I set the preferredDisplayMode as UISplitViewControllerDisplayModeAllVisible in the master viewController.

I have these method on the master view controller:

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

//This is called

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

 //This is not called

    [self.searchController.searchBar sizeToFit];

    [self configNavBar]; //Adds few buttons to the nav bar

}

I'm wondering why the viewwillAppear isn't called here but viewDidLoad, viewDidAppear are called. The breakpoint in the viewWillAppear didn't hit .

If I set the preferred display mode as UISplitViewControllerDisplayModePrimaryOverlay, then viewWillAppear is getting called. But here the detail view controller doesn't occupy the half screen.

Upvotes: 5

Views: 532

Answers (1)

Y.Bonafons
Y.Bonafons

Reputation: 2349

I know its an old one but I have the same problem here. On iPad, no problem, the viewWillAppear is called but on iPhone it is not called at all the first time i display the controller.

I finally found that it was due to the call of this lines of code:

if let target = displayModeButtonItem.target, let action = displayModeButtonItem.action {
    UIApplication.shared.sendAction(action, to: target, from: view, for: nil)
}

which was called after the initialization of my UISplitViewController.

When I removed this, it worked. But I was also able to bypass it by using the mode: .primaryHidden on iPhone (I dont use the iPhone on landscape thats why my condition on this example will be iPhone / iPad). So my custom UISplitViewController look like this:

override func viewDidLoad() {
    super.viewDidLoad()
    self.preferredDisplayMode = UIDevice.current.userInterfaceIdiom == .pad ? .allVisible : .primaryHidden
}

Hope this could help someone.

Upvotes: 1

Related Questions