iZettle / Presentation

Presentation is an iOS Swift library for working with UI presentations in a more structured way
MIT License
73 stars 11 forks source link

Remove the fatal error in sheet presentation when optional view is not passed #69

Closed nataliq-pp closed 4 years ago

nataliq-pp commented 4 years ago

What?

Removed the fatal error for sheet presentation with non-nil popoverPresentationController without a sourceView. Now using the presenting's view controller view as a source view and the source rect is in its center.

Now it works like this with the styles demo app if I change the sourceView here to nil: Jun-10-2020 14-36-05

Why?

The example above from the demo app was causing a crash.

The fatal error is redundant and premature.

Redundant because UIKit already crashes with a pretty explanatory message anyway:

Screenshot 2020-06-09 at 09 29 36

Premature because we're checking the source view at the point of initialisation but it might not be used. For example, on iPad with compact horizontal class (half screen, landscape, multitasking) even though the modalPresentationStyle of an UIAlertController created with preferred style actionSheet will be popover, the resulting presentation won't be a popover but a bottom sheet.

I understand that it's better to crash earlier if we know that accessing something at a later point will cause a crash but in this case we don't seem to be getting much from the crash. If we wanted to notify earlier about an API misuse we could make sourceView non optional parameter for the presentation style but that will make the API weird as you can show a sheet without a source rect anyway. Thus I'm adding a fallback for the popover anchor location to be the presenting's view centre.