Swift Navigation bar background color white does not work - swift

Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
Above line doesnt work just for white.
Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...
Please advice where I am going wrong...
Thanks.

Try this Code:
In your viewDidLoad:
title = "Some Title"
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
//Line under NavigationBar
let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
barView.backgroundColor=UIColor.red // set any colour you want..
navigationController?.navigationBar.addSubview(barView)
//Title Colour
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
Note:
Above code works on any background colour....
If you want the navigationBar to be green....set your view background colour to green...

Related

How can I make the UISearchController SearchBar background color the true, untinted color?

I tried virtually every solution I could find for changing the background color of a UISearchController SearchBar, but none of them produced the correct color as the background. Every solution produces a somewhat darker color, and as demonstrated in the image below, white seems more pale / off-white.
How can I make the search bar a true white color?
One of the more recent "solutions" that results in white being pale is below:
let searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
let textFieldInsideSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.backgroundColor = UIColor.white
}
The default border style is being impacted by the navigationItem. If you remove the border style and manually set the corner radius, it should display as white. White seems to be the only color I ran into being affected by this.
Change:
textFieldInsideSearchBar?.backgroundColor = UIColor.white
To:
textFieldInsideSearchBar?.borderStyle = .none
textFieldInsideSearchBar?.cornerRadius = 10
textFieldInsideSearchBar?.backgroundColor = .white
When I put the search bar in a table header, your code above (without altering the border/radius) worked without issue, but when I tried to embed it into a navigation bar item, I ran into your problem.

How to create navigation bar translucent effect?

I have the table view with headers. Headers have only custom text and white background. Headers are pinned on top when scrolling and I want to know, is there any way how to make the background of headers translucent same as navigation bar is? With some blur effect?
Thanks for any advice!
Change the backgroundColor to .clear, add a UIVisualEffectView below the label and set the constraints to the edges. Not sure if it will mimic the behaviour of navbar by default so you would have to play around a little with the UIBlurEffect options to get the desired effect.
let blurEffectStyle = UIBlurEffectStyle.extraLight
let blurEffect = UIBlurEffect(style: blurEffectStyle)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
Then add to view and set the constraints
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.view.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.backgroundColor = UIColor.clear

UISegmentControl do not want fade when disabled

I have a Swift 4 UISegmentControl and I do not want it to fade when disabled.
anyone know how to stop if from fading?
tried this suggestion ... this changes the color ok, BUT is still faded
for view in mySegment.subviews {
view.tintColor = UIColor.blue
}
tried this suggestion ... changes color of text only for unselected AND it is still faded
let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
mySegment.setTitleTextAttributes(titleTextAttributes, for: .disabled)
I want to set the color and NOT have the color fade

The title of my UIButton with UIVibrancyEffect is not visible when backgroundColor is assigned

I have two buttons added to UIVisualEffectViewwith effect UIVibrancyEffect:
let vibrancyEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .Dark)))
vibrancyEffectView.contentView.addSubview(rightActionButton)
vibrancyEffectView.contentView.addSubview(leftActionButton)
the nest is set Title for them:
rightActionButton.setTitle(rightAction.title, forState: .Normal)
leftActionButton.setTitle(leftAction?.title, forState: .Normal)
And backgroundColor:
leftActionButton.backgroundColor = UIColor.clearColor()
rightActionButton.backgroundColor = UIColor.whiteColor()
Why I cannot see the right title? How to fix this?
Below is example from Apple, exactly what I need to do:
Is it possible?
Just set a backgroundColor with alpha component.
let vibrancyEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .Dark)))
leftActionButton.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
rightActionButton.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
And the result is following:
The title is there but you cannot see it because it has the same color as it background. To use UIVibrancyEffect button background color must be set to clear color so this way all transparent pixels that are multiply with their corresponding background pixels will result in transparent as well. To fix this:
rightActionButton.backgroundColor = UIColor.clearColor()

Change UIBarButtonItem from UISearchBar

I'd like to change the text font and color of the Cancel button inside the UISearchBar in iOS 8. I've tried the solutions for iOS 6 and 7 already and they don't seem to work.
This was not the solution I was looking for, but it worked.
let cancelButtonAttributes: NSDictionary = [NSFontAttributeName: FONT_REGULAR_16!, NSForegroundColorAttributeName: COLOR_BLUE]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, forState: UIControlState.Normal)
What you are looking for is:
//The tintColor below will change the colour of the cancel button
searchBar.tintColor = UIColor.blueColor()
But these are also useful While we are on the subject:
searchBar.placeholder = "Placeholder"
//The barTintColor changes the colour of the flashing text indicator
searchBar.barTintColor = UIColor.redColor()
//This sets the cancel button to show up
searchBar.setShowsCancelButton(true, animated: true)
//This makes the searchBar become active
searchBar.becomeFirstResponder()