uisearchdisplaycontroller dimming view color - uisearchdisplaycontroller

I am trying to get color of uisearchdisplaycontroller dimming view. I am trying like this. But it is not like that color. May I know what is the color? Does it depend on each ios version as well?
[self setBackgroundColor:[UIColor colorWithWhite:0.05f alpha:0.1f]];

UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
Black color with 40% opacity

Related

Create overlay with scroll enabled in Swift 3

I have a view with a UICollectionView (with scroll enabled).
I would like that, when I click on a button, there is a black overlay shown behind a menu.It would be better with a picture:
The problem is that this overlay is not covering all the background when I scroll down.
When the scroll is up, it's perfect, but when scroll down we can see the limit of the subview.
This is my code:
let width = UIScreen.main.bounds.height
let height = UIScreen.main.bounds.height
overlay = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height))
overlay.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3)
overlay.layer.zPosition = 1
// AND WHEN THE BUTTON IS PRESSED
// self.collectionView.addSubview(overlay)``
How could I correct this problem ?

Status bar and navigation bar does not seems to have the same color iOS Swift

I want to set the status bar color the same color as the navigation bar. when I try to set the navigation and status bar to the same color the navigation bar always appear in a lighter color than the status bar.
This is what I want:
My result:
Code in AppDelegate:
Status bar:
UIApplication.sharedApplication().statusBarStyle = .Default
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector(Selector("setBackgroundColor:"))
{
statusBar.backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
statusBar.tintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
}
Navigation bar:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .Default
Could anyone give me any suggestion on how to solve this problem.
Thanks in advance!
Warning! Do not do any of this in iOS 13 or later, and definitely not in iOS 15 or later. This is outdated code from long ago!
I was able to get the desired result:
Here's the code I used (Swift 3):
let app = UINavigationBar.appearance()
// nav bar color => your color
app.barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
app.isTranslucent = false
// status bar text => white
app.barStyle = .black
// nav bar elements color => white
app.tintColor = .white
app.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
This line in your code is illegal and will likely get your app banned from the App Store:
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
For the status bar, its color in iOS these days is clear. There is no need to set its color, and you are not supposed to do so. The navigation bar is extended up behind the status bar, and thus they will always have the same color because what you are seeing is always the same interface object, the navigation bar.
As for setting the navigation bar's color, don't forget that it is translucent by default. You cannot set its color accurately without making it opaque. Also, you should not be setting its backgroundColor. You should either set its barTintColor or else give it a background image (that is the way to get the best control over its color).
For me the key was to change the isTranslucent attribute:
navigationBar.isTranslucent = false
navigationBar.barTintColor = PlateColors.mainRed
navigationBar.backgroundColor = PlateColors.mainRed
To achieve the desired result, set the status bar style to default and set the UINavigationBar.appearance().barTintColor to the required color.
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
UIApplication.sharedApplication().statusBarStyle = .Default
based on #matt suggestion to extend navigationbar below status bar, navigation bar must be opaque:
let navigationBar = navigationController?.navigationBar
navigationBar?.isOpaque = true
navigationBar?.setBackgroundImage(UIImage(color: UIColor(white: 0, alpha: 0.5)), for: .default)
navigationBar?.shadowImage = UIImage()
extension UIImage {
convenience init?(color: UIColor) {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
}
For anyone who ended up here a little later, like myself (Xcode 13.1, iOS 15.1), this may help:
In iOS13 the status bar background colour is different from the navigation bar in large text mode
It's all about defining the appearances for both standardAppearance and scrollEdgeAppearance and setting their respective background colors.
Personally, did not even need to solve this programmatically:
Navigation Bar Attributes
I have encountered the same problem and I have found that it is not the problem of the status bar but the navigation bar. If you set the background color of navigation bar and a simple UIView to the same, the real color shown will be different. To make the color of navigation bar same as another view:
Wrong Code
navigationBar.backgroundColor = .blue
view.backgroundColor = .blue
Right Code
navigationBar.isTranslucent = false
navigationBar.barTintColor = .blue
view.backgroundColor = .blue
In this case, their color will be the same.

Swift - Custom color not applying to barTintColor [duplicate]

This question already has answers here:
UIColor not working with RGBA values
(6 answers)
Closed 6 years ago.
In a view controller embedded in a navigation controller I am attempting to change the barTintColor to a custom color. What I have experienced is if I use a default color such as the one below, the color is actually applied:
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
However when I try to create an instance of my own custom color such as this code would show, the color is not applied:
let customRedColor = UIColor( red: 255, green: 0, blue: 13, alpha: 1 )
self.navigationController?.navigationBar.barTintColor = customRedColor
I am very curious as to why the custom color is not being applied to the navigation bar so that leads me to ask:
What approach should be taken in order to correctly apply a custom color to the navigation bars barTintColor property.
You need to give color by dividing them by 255, the syntax is like this.
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Each of the ones that take a CGFloat take a value between 0.0 and 1.0, referring to either the complete absence of, or the maximum amount of that color component respectively. So, this means that even if you have pure RGB values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.
let customRedColor = UIColor(red: 255/255.0 , green: 0, blue: 13/255.0, alpha: 1.0)
//or Direct
let customRedColor = UIColor(red: 1.0 , green: 0, blue: 0.05, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = customRedColor

Swift Alert Controller Color

I have an alert controller that I set to a new tint color with this line of code:
alertController.view.tintColor = UIColor(red: 0.59, green: 0.59, blue: 0.59, alpha: 1.0)
The color does show correctly, but once the alert action is tapped, it changes back to the default blue color automatically. How can I make it so that the tint color does not change back to default. I have heard of the func tintColorDidChange() but I am unsure of how to use it?
You can try this. It has been discussed here
you should add the same code in presentViewController completion handler
presentViewController(alert, animated: true) {
alert.view.tintColor = UIColor(red: 0.59, green: 0.59, blue: 0.59, alpha: 1.0)
}

Change background color of single bar bar item - swift?

I have a UITabBar of which I want to change the background color of the middle item, but I can't figure out how! (I want to keep the rest of the bar the dark grey color it is).
let barTintColor = UIColor(red: 54/255, green: 54/255, blue: 54/255, alpha: 1.0)
UITabBar.appearance().barTintColor = barTintColor
You can do this by inserting a new subview to your TabBar.
Please check out this answer:
// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)
let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)
Hope it helps.
Edit:
If you want to change the background image rather than background color, all you have to do is change the line :
bgView.backgroundColor = bgColor
to imageView with image as background, then add it as a subview. It may look like this:
backgroundView = UIImageView(image: UIImage(named: "tabBarImage"))
bgView.addSubview(backgroundView)