Recently, I have been working on my app update. In some cases, some actions could not be performed. I need to present a view to explain the reason. Alert view is a very common method to do that. Alert view is good to present the information, however, it is a modal view. User have to tap on OK or Cancel button to dismiss the alert view. I prefer the method of a popover view as alternative.
It is very easy to do that by using the same UIActionControler
, and set the style as .actionSheet
. This is well explained in Nick Meehan's post. Still there some thing missing is his post.
I would like to support both iPhon and iPad. For iPhone case, an action button should be added to dismiss the view. By default, the view is still a model view. You need a way to dismiss the alert view.
It is very interesting to find out that an action with .cancel
style will change the modal view into a popover view. Further investigation, there is no need to add action handler to dismiss the view. The actionSheet will behaviour like a popover in iPhone.
Here are some codes.
- if let popoverController = alertController.popoverPresentationController {
- popoverController.sourceView = view
- popoverController.sourceRect =
- CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
- popoverController.permittedArrowDirections = []
- } else {
- let cancelAction = UIAlertAction(title: UIHelper.dismissButtonTitle(), style: .cancel)
- alertController.addAction(cancelAction)
- }
Another interesting thing is that the cancel style action button will be displayed as an separate button on the bottom. You can specify any title to the button. I like it. This is the UI as in some Apple apps, such as deleting a message in message app.
Here is what I achieved in my update:
References
- Nick's blog: ActionSheet Popover on iPad in Swift
- My App: TapToCount-3W
0 comments:
Post a Comment