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

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()

Related

How can I change the font and color of the Navigation Bar back button

I am trying to change the font and color of the Navigation Bar button.
Ideally, I'd like to change it to black and use a custom font that I already have set up. Is there any way to modify the existing button?
I tried this in my view's init for the font
UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
But I think this only changes the title of an inline Nav bar
It's a UIBarButtonItem so set this property.
init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}
Or you can use this also,
init() {
let standard = UINavigationBarAppearance()
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}
You can do the method above or:
Go to your Assets -> Set a colour for Accent, although this will affect other components in your app i.e. DisclosureGroup button etc.

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

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

Swift Navigation bar background color white does not work

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

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()