Swift : OverlayView that appears at the bottom of the screen - swift

I'd like to create a very common effect as in the picture below :
Explanation : the effect I'd like to accomplish consists in a view that appears (slides in) at the bottom of the screen when user clicks a button : you can still see the screen behind this view, but it applies a "dark layer" (black view with let's say 60% opacity) on the top of it. When user clicks on Block or Report absue (as for this example) it would perform the respective actions. Now when user clicks on cancel, and also when he clicks anywhere on the "dark layer" it would bring him back to the screen.
What I tried : presenting a new view controller (but it would use more data than necessary), using a overlay layer but I even didnt get close to that effect that we're usually seeing on apps. I'm not sure but I'd say that the best way to get that effect is using views ?
Does anyone have an idea please ?
Thanks and have a good day,
J

You are looking for a native element called UIActionSheet. It has become the part of UIAlertController, and does exactly what you are looking for.
Here is a little help how to set up one:
// Create you actionsheet - preferredStyle: .actionSheet
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create your actions - take a look at different style attributes
let reportAction = UIAlertAction(title: "Report abuse", style: .default) { (action) in
// observe it in the buttons block, what button has been pressed
print("didPress report abuse")
}
let blockAction = UIAlertAction(title: "Block", style: .destructive) { (action) in
print("didPress block")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("didPress cancel")
}
// Add the actions to your actionSheet
actionSheet.addAction(reportAction)
actionSheet.addAction(blockAction)
actionSheet.addAction(cancelAction)
// Present the controller
self.present(actionSheet, animated: true, completion: nil)
It should produce the following output:

Related

Disable the auto dismissal of the alert when tapping on a button

func showAlert(...) {
let alertController = UIAlertController(...)
let add = UIAlertAction(title: "Add", style: .default) { (action) in
onAdd?()
}
alertController.addAction(ok)
...
}
I would like to keep the popup alert after click on Add. But it seem iOS will auto close the popup. How to make it work?
The only way to prevent a UIAlertController's alert from being dismissed when a button is tapped is to disable the button.
If you don't like that, don't use UIAlertController; use your own presented view controller. That's all a UIAlertController is, after all. So it's easy to write your own if you need to.

How to trigger a notification if label is dropped on wrong target?

In this drag and drop game (adapted from this tutorial), the player has to match the label to the right target. I have it set so that if the touch ends and the label's center is inside the target, it is removed from the screen and the game starts over.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if label.name == "letters" {
if lettersBin.frame.contains(label.position) {
// remove it and create a new label
label.removeFromParent()
setupDragLabel()
However, I want to trigger a notification if the player drops the wrong label onto the bin, so:
else if label.name == "numbers" {
if lettersBin.contains(label.position) ...
What would I write to finish this? Do I make another label appear on the screen?
It might be easiest to use a UIAlertController. You can add the messages along with button actions to display that the user was wrong.
let converterAction = UIAlertController(title: "Your title here", message: "Your message to the user here", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default){ _ in
//Any custom action to happen here
}
converterAction.addAction(okAction)
present(converterAction, animated: true, completion: nil)
If you do not want to show an alert, you can do the following:
Add a label to the Storyboard and to your file.
Set that label to be hidden by default.
Programatically set label.isHidden = false and set it's text as you wish.
What I now labeled "label" can be anything though, you could even create a group of views and hide/show them all at once. Useful when wanting to show a Progress Indicator and a "Loading..." Label at the same time.

How to change background of UIAlertController in Xcode 8

My situation is that I want to change the background color of the alerts as well as the text. I've seen some examples, but they don't appear to be working anymore. Here is what I've tried:
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: btnTitle, style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
let subview :UIView = alert.view.subviews.last! as UIView
let alertContentView = subview.subviews.last! as UIView
alertContentView.backgroundColor = UIColor.black
I have also tried to do this:
alert.view.backgroundColor = UIColor.black
and also putting them in the completion handlers, but it doesn't appear to be working like previous versions. What is appearing instead is the alert with the usual rounded corners and outside the rounded corners, the black background appears in the back to make the alert a square box (normally its rounded and the part where the black is showing would be transparent). The color just won't go over the alert's white color.
you have to change the color(alert.view.backgroundColor = UIColor.green) before you do self.present(alert, animated: true, completion: nil)
It Works for me like that.. i have used green and red

Execute handler function after tap on UIAlertAction of UIAlertController *SpriteKit*

I am trying to get a UIAlterController to display (as soon as the scene is set up) with the following buttons:
Accept - won't do anything, just close the UIAlertController
Leave to Menu - should transition to the MenuScene
My code looks as follows (I put the alert controller inside an action so it first displays the scene and after 0.5 seconds it should start displaying the alert):
//make action
var actionBlock = SKAction.runBlock({
//alert controller
var alert = UIAlertController(title: "Welcome", message: "Are you sure you want to proceed?", preferredStyle: UIAlertControllerStyle.Alert)
//action with no handler
var acceptAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Cancel, handler: nil)
//action thats supposed to transition to MenuScene (AFTER BEING TAPPED!!)
var leaveAction = UIAlertAction(title: "Back to Menu", style: UIAlertActionStyle.Destructive, handler: {_ in self.backToMenu()})
//add action to the alert controller
alert.addAction(acceptAction)
alert.addAction(leaveAction)
//add alert controller to view
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
//wait for 0.5s action
var wait = SKAction.waitForDuration(0.5)
//run the action
self.runAction(SKAction.sequence([wait, actionBlock]))
and I placed it right into the DidMoveToView function. When I run the code, it immediately transitions back to the MenuScene without even waiting for the user to tap either one of the actions, and then the alert is still on the screen (but on the MenuScene).
I am really confused and I've been on this for several hours but I just can't figure out what my mistake is, and how to make it work..
Anybody help please? Any code that works would be really appreciated!!!

UIAlertAction dismisses the UIAlertController in its callback [duplicate]

This question already has answers here:
Prevent dismissal of UIAlertController
(5 answers)
Closed 8 years ago.
I am trying to use new UIAlertController introduced in iOS 8. Everything works great except the fact that UIAlertAction always end up dismissing the alert controller in its callback. Following is my code:
let alert = UIAlertController(title: "New Group", message: "Enter Group name", preferredStyle: UIAlertControllerStyle.Alert);
alert.addTextFieldWithConfigurationHandler({ [weak self] (nameField: UITextField!) in
nameField.becomeFirstResponder();
nameField.delegate = self;
return;
})
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: { action in
println("Done Entering");
}));
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil));
self.presentViewController(alert, animated: true, completion: nil);
Now, when I click "Done" button, the controls enters the callback method and then alert is dismissed even though I don't have any statement to dismiss the alert. Is this behavior by default? If yes, how can I make sure that in some cases the alert stays on the screen (depending upon my conditions)? Am I missing something here?
I would really appreciate any help regarding this.
Yes, alert buttons always dismiss the alert and there is no way to configure them otherwise. If you want that behavior, you'll have to write your own alert. I've written SDCAlertView, which looks very much like a normal alert, but has some added functionality, including prevention of dismissing an alert when a button is tapped.
However, it doesn't use the UIAlertController API yet and it looks a little bit different (most users won't notice) on iOS 8 than the UIAlertController alert.
EDIT: It now has support for the UIAlertController-like API.