How to change background of UIAlertController in Xcode 8 - swift

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

Related

Multi-line UIAlertController freezes on macOS Ventura

After upgrading to macOS Ventura I can no longer have multi-line alert views with a scroll bar. The dialog will not show and the app freezes.
let myAlertTest = UIAlertController(title: "TITLE", message: "If this line is too long, the app will freeze instead of showing scroll bars. Worked fine with Monterey", preferredStyle: UIAlertController.Style.alert)
myAlertTest.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(myAlertTest, animated: true, completion: nil)

UIAlertController does not turn all elements in the background to B/W

EDIT: Repository with my solution: UIAlertControllerDimmed
After showing UIAlertController, most of the background gets 'dimmed' and turns black and white. Some elements get darker, but don't turn B/W.
These elements are (from top to bottom on the screenshot):
UIImageView inside UINavigationItem
UIButton with red background color and white image
UIImageView inside UITabBarItem
I couldn't find anything related to this topic. What do I have to change to also get these items dimmed?
Here is the without the UIAlertController:
]
I think what's going on here is that you're setting the tintColor of some of the elements and you get different behavior for tintColor than you do for backgroundColor or textColor (or the colors in an image).
When an alert or action sheet appears, iOS 7 automatically dims the
tint color of the views behind it. To respond to this color change, a
custom view subclass that uses tintColor in its rendering should
override tintColorDidChange to refresh the rendering when appropriate.
For example, I created a simple app that displays an alert controller. I set the left button tint color to clear color and the text color to blue:
I set the right button tint color to system green color:
When I run the app and present the alert controller it looks like this
Before:
After:
In order to get the behavior you're looking for, you'll need to follow the advice in #Alexander's answer. You'll need to create grayscale versions of the four images on the screen and animate the transition to them.
You can have a helper function to animate the color change
fileprivate func dimElements(highlight: Bool) {
UIView.animate(withDuration: 0.3) {
self.sendButton.backgroundColor = highlight ? .red : .gray
}
}
and then call it when presenting/dismissing the alert.
let alert = UIAlertController(title: "Error", message: "Oops!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .cancel, handler: {_ in self.dimElements(highlight: true) })
alert.addAction(okAction)
self.dimElements(highlight: false)
present(alert, animated: true, completion: nil)
Thank you for your help.
In order to have a more flexible solution I decided to create a subclass of UIAlertController which captures a screenshot, turns it to grayscale colors and inserts it behind the UIAlertController when it gets presented. This way it works without having to do any additional work, and you don't need to implement fade animations for every single element that does not turn to grayscale colors by default.
Github repo: UIAlertControllerDimmed

swift xcode change size for navigationItem.leftBarButtonItem = UIBarButtonItem

wanting to set the size for the image that show in the simulator as it comes up far to big i've tried many thing to no avail ?
func CancelButton(){
navigationItem.leftBarButtonItem = UIBarButtonItem(image:
#imageLiteral(resourceName: "HOME BUTTON BLACK").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleCancel))
}
func handleCancel() {
self.dismiss(animated: true, completion: nil)
}// handleCancel
You need a smaller image!
You can either do this in code, or replace the current image with a smaller one.
For bar button items, the best size is around 25x25, so try to resize it to that.
To do it in code, you can refer to this answer.

Swift : OverlayView that appears at the bottom of the screen

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:

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!!!