0

I have added a child UIViewController's view to a UIViewController on the storyboard from code instead of using segue. The custom UIViewController contains UISearchController. Following is the code I am using for adding the custom/child UIViewController:

self.customController = [[CustomUIViewController alloc] init];
[self.searchBarContainerView addSubview:self.customController.view];

'self.searchBarContainerView' in the above code is an UIView for displaying search bar of UISearchController. This code is working fine and the search bar is shown without issues at runtime. However when I am clicking inside search bar, following warning message is logged in the output window

"presenting view controllers on detached view controllers is discouraged "

I need help is fixing this warning from being shown.

Bhavesh Nayi
  • 705
  • 4
  • 15
D Surya Praveen
  • 321
  • 3
  • 17
  • you are trying to present uisearchcontroller on CustomUIViewcontroller, which is not the root controller. Hence the warning message occurs. – Teja Nandamuri May 02 '19 at 13:39
  • @TejaNandamuri When adding UISearchController to root controller directly I am facing issue with search bar stretching out of table view's header view on clicking in search bar. That's the reason I have added UISearchController in a custom UIViewController. So that search bar is not exceeding width of UIViewController. See this [question](https://stackoverflow.com/questions/55704967/how-to-resizing-issue-of-uisearchcontroller-searchbar-when-focused) which I posted earlier – D Surya Praveen May 02 '19 at 13:44
  • I'd suggest using UISearchbar instead of using UISearchController to avoid these issues. – Teja Nandamuri May 02 '19 at 13:48
  • @TejaNandamuri I know UISearchbar would be better, but I am trying to use latest standard UISearchController. There are issues with UISearchController which may be fixed in future Xcode versions, meanwhile I am looking for any workarounds. – D Surya Praveen May 02 '19 at 13:53

1 Answers1

0

As Cy-4AH suggested and as per the link I have added following steps and it worked.

[self addChildViewController:self.customController];
self.customController = [[CustomUIViewController alloc] init];
[self.searchBarContainerView addSubview:self.customController.view];
[self.customController didMoveToParentViewController:self];
D Surya Praveen
  • 321
  • 3
  • 17