WithPopover

In this last post I‘ve proposed an OutlinePicker for SwiftUI. But, to be honest, I am not quite satisfied with this solution. The selection view appears as a sheet in contrast to the standard picker and its layout deserves some more fine tuning.

The problem with the standard popover modifier in SwiftUI is that it’s mutated to a sheet presentation on iPhones. I know, there is this new modifier presentationCompactAdaptation(horizontal:vertical:) but that’s only available since iOS16.4. In addition, I want to use a List in the popover and that’s collapsed to a tiny square if you do not specify a frame size. To make a long explanation short: I prefer to use the nice utility view „WithPopover“ for the presentation of my picker selection.

You will find this nice utility here but it has two problems. First of all it’s not updating properly when the popoverContent is changed while being used by WithPopover. This is fixed by some careful reader. Thank you for that.

The other problem is a quite penetrant warning by the OS that the presentation of a view from a detached view controller is discouraged. Well, it’s not obvious that the view controller of WithPopover is detached from the view hierarchy. Anyway, the situation is getting worse since at least the beta version of XCode 15 / iOS 17 is stating that this will be a real exception in future releases. Not so nice.

So, if the view controller of WithPopover is detached from the view hierarchy maybe we can find a view controller that is not. Well, there are these nice two extension which give the topmost view controller:

The first one is a replacement of the old keyWindow property and the second one returns the topmost view controller for that. So, if we present the popover from that view controller the warning is gone. Here is my modified version of WithPopover:

Lines 42-43 reflect the modifications from cloxnu and lines 71-72, 81 are my additions to present the popover from the topmost view controller.

In my next post I will present a modified version of NodeOutlineGroup.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert