I'm adding a view to the window with this objective-c code:
// Main Storyboard
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
// View controller with the view to add
UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"SomeViewController"];
// Find the topmost window
UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) {
return win1.windowLevel < win2.windowLevel || !win1.isOpaque;
}] lastObject];
// Add the view to the window
[topWindow addSubview:vc.view];
This works fine and so does this Swift method I use in SomeViewController
to leave the view:
// Get topmost window (Should be UIWindow)
let topWindow = UIApplication.shared.windows.sorted {
(win1, win2) in
return win1.windowLevel < win2.windowLevel || !win1.isOpaque
}.last
// Force view to prepare to disappear
self.viewWillDisappear(true)
// Send the view to the back
topWindow?.sendSubviewToBack(view)
This is the structure of my views after the objective-c code runs. The selected view is the one that got added:
The issue is none of the buttons in the new view work so I can't trigger the above close method. I've tried both buttons added with the storyboard and programmatically added buttons. Visually the buttons react when tapped but they won't trigger anything.
I ended up resolving the issue by completely swapping out the root view controller rather than simply adding it's view as a subview. The problem was as @DonMag pointed out that I was adding the subview without it's view controller. I found this question about swapping the root view controller and used that as a basis for my changes. If someone wants to provide a complete solution that doesn't require swapping the root view controller I'll accept that instead.