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
Related
I know a lot of people asked this question, but nothing helps. My controller1 presents a new controller2 from storyboard. Controler1's background color is black. My customized segmentcontrol background color and selectedTintColor work well, but always show a black background color first. I tried every way, but not working. In storyboard and code, all no this black color. Thanks!
override func viewDidLoad() {
let bgColor = uIColor. // my customized color)
segmentControl.backgroundColor = bgColor
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
connectionSegmentControl.selectedSegmentIndex = 0
toggleSegmentTintColor()
}
private func toggleSegmentTintColor() {
// I set my customized selected tint color here, it works.
}
If you do this in viewDidAppear, its too late: the view is on the screen. The hack way to do it is in viewDidlaod or viewWillAppear. The right way to do it is to to use UIAppearance proxy on app launch to set the tint color for your object globally:
UISegmentedControl.appearance().tintColor = UIColor.clear
You can also set the tint color for the whole app by setting it on the root window, but then it affects more than just the segmented controls.
In iOS 13 back button title color not dimmed whenever we open popover or present other controller. Other part of application grayed out but not back button.
also tintColorDidChange function is not available for UIBarButtonItem so is there any way to do grayed out all back button titles.
Note: Back button image is being grayed.
Have you try by changing tint color of bar button?
I got same problem and solved by using
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
self.navigationController?.navigationBar.tintColor = .red // put your desire color
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
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
I know that we can set a colored title for a button using attributed strings like this:
let style = NSMutableParagraphStyle()
style.alignment = .Center
button.attributedTitle = NSAttributedString(string: "title",
attributes: [ NSForegroundColorAttributeName : NSColor.redColor(),
NSParagraphStyleAttributeName : style])
In fact that's how I'm getting the red title to begin with, but it has no effect on the title color in the disabled mode (it is always gray); I guess in order to set that, I have to create an instance of the NSButton class and override some of its methods related to title color or override some of its properties to disable user interactions(when necessary) instead of disabling the button, but I don't know how.
There are some possible objective-C answers to this question here, here and here.
Create a NSButtonCell Subclass and assign it to the CELL (not the button) in IB.
Then implement
func drawTitle(_ title: NSAttributedString,
withFrame frame: NSRect,
in controlView: NSView) -> NSRect
and return different attributedString for isEnabled or !isEnabled.
You might also set the initial attributedTitle in
init (id)
EDIT: Only works if setWantsLayers is false
Do you need it to be disabled by the definition of it not doing anything when touched?
If so, just have a flag in the button clicked function. So then, the button color could be red (example) when disabled but you can click it but nothing happens. Then if it it's green (example) then it'll allow it to do something.
Your flag could be the color of the button checked through an if statement or set an actual variable.
I'm writing an iOS 8 app and this problem is driving me crazy... I've set the tint color of my window in white (because I've got colored toolbar and navigation bar) but now, when in my WKWebView I long press on a Link, the actions titles of the Action Sheet that appears are written in White and so it's very difficult to read them.
Is there a way to change the tint color of this object?
I tried with
UIActionSheet.appearance.tintColor but it doesn't work, perhaps that control is not a UIActionSheet?
Thank you
Dave
I haven't found a solution but I've found a workaround: I set the tint color of the window as I like and then I set just the navigation bar's tint color and the toolbar's tint color in white.
This work for me, Swift 2.1 xCode 7 :
let actionSheet = UIActionSheet(title: "Launch your GPS navigator", delegate: self,
cancelButtonTitle: "Cancel", destructiveButtonTitle:
"Cancel", otherButtonTitles:"Open path in Google Maps"
, "Open path in Maps", "Open path in Waze")
if actionSheet.respondsToSelector("_alertController") {
let alertController = actionSheet.valueForKey("_alertController")
if ((alertController?.isKindOfClass(UIAlertController)) != nil) {
alertController?.view??.tintColor = UIColor.redColor
}
}
actionSheet.showInView(self.view)