UINavigationBar: Set colors in subclass - ios

I have the following subclass:
class GeneralNavigationBar: UINavigationBar {
override func layoutSubviews() {
super.layoutSubviews()
self.barTintColor = UIColor(rgb: 0x2A5298) //Extension that converts hex to color
self.tintColor = UIColor.white
}
}
I want the title of the bar to be white. When I apply this class to a NavigationBar in the storyboard, the background gets blue (hex, as he is supposed to), but the title remains black.
It's strange, since you can alter the color of the bar in the ViewController it appears in:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
This works.

Try appearance,see how..
var navbarappearace = UINavigationBar.appearance()
navbarappearace.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
navbarappearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
Put the above code in AppDelegate, it should affect the whole project.
Cheers.

Related

UINavigationBar tintColor is misbehaving

I have issue when set UINavigationBar tintColor.
It is misbehaving
I'm using xcode 11.3.1, swift 5, iOS 13.3
*MyClass
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "test"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
}
*result
real device
https://imgur.com/sONr4vq
simulator
https://imgur.com/vs5lhgR
I want to set back button color like title
I only get error on real device
Please help me
Thank you
To set back button and hide "back":
let backItem = UIBarButtonItem()
backItem.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
navigationItem.backBarButtonItem = backItem
You should but this code to the ViewController before push the new one.
Try access to navigationController.navigationBar instead of navigationController.view :
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Did you try using UIColor instead of #colorLiteral?
Like
self.navigationController?.navigationBar.tintColor = UIColor(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
//or
self.navigationController?.navigationBar.tintColor = UIColor(displayP3Red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Edit:
Since that the above did not work for you, did you try this?
self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationItem.titleView?.backgroundColor
Considering that all you want is to have both the button and title to have the same colour right?

Different colours for status bar and for navigation bar but actually they should be the same

I have this extension which allow me to have some UIView properties (for example backgroundColor):
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
Then I write this code in application(didFinishLaunchingWithOptions:)
UIApplication.shared.statusBarView?.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
As you can see colours are the same, but result is strange because in fact they are different:
See the difference between two green colours?
Why this difference happens?
Thank you!
Try to set the following value:
UINavigationBar.appearance().shadowImage = UIImage()
If this does not help, try playing around with the following commands (I use them in my apps to set everything to white background):
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = false
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor.white

How would I change background color of alert view

I am working on tvOS project, I need to create custom alert view with particular background color and text.
Finally , I have got the solution,
if let next = context.nextFocusedView as? UIButton {
next.setTitleColor(UIColor.blackColor(), forState: UIControlState.Focused)
next.backgroundColor = UIColor(red: 248/255, green: 175/255, blue: 2/255, alpha: 1.0)

PushViewController & set navigation bar color and title

I made a push function in a button:
#IBAction func prodButton(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = CollectionViewController()
secondViewController = storyboard.instantiateViewControllerWithIdentifier("CollectionViewController") as! CollectionViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
This button pushes to the secondViewController but when I looked at the navigation bar of the second view controller I noticed that it has set up a back button automatically. The problem is that this back button's color is light blue and it doesn't fit with my design. I tried to change it like that in viewDidAppear
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.redColor()
and also the bar color:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
but there wasn't any change.
I'd be really thankful if somebody help me with that.
Thanks in advance.
Try to set your color one time, it will be the same color for everywhere after:
let navBar = UINavigationBar.appearance()
navBar.tintColor = UIColor.redColor()
You should put this code in AppDelegate. You could also set the barTint in Storyboard if you use it.
Use
//Func Alter color navigation bar
func AlteraCorNavigationBar(Navigation:UINavigationController){
Navigation.navigationBar.tintColor = CorTextoNavigationBar()
//Navigation.navigationBar.barTintColor = CorPredominante()
Navigation.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : CorTextoNavigationBar(), NSFontAttributeName : UIFont(name: "ArialHebrew-Light", size: 22)!]
}
//func CorPredominante()->UIColor{
//return UIColor.redColor()
//(rgba: "#ecf0f1")
// YOU CAN LIBRARY COLOR RGBA [HERE][1] AND USE UIColor(rgba: "#b6011a")
//}
func CorTextoNavigationBar()->UIColor{
return UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
}
for called in view controller:
class NewViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AlteraCorNavigationBar(navigationController)
}
}
I found the mistake.
The colors shoud be devided by 255:
UINavigationBar.appearance().tintColor = UIColor(red: 149.0 / 255.0, green: 148.0 / 255.0, blue: 192.0 / 255.0, alpha: 0.5)
UINavigationBar.appearance().barTintColor = UIColor(red: 65.0 / 255.0, green: 61.0 / 255.0, blue: 116.0 / 255.0, alpha: 1.0)
Thank you all for the help ;)

iOS UINavigationBar Style issues

I have an issue with UINavigationBar styling:
1) I have applied style change in the AppDelegate.swift:
//Change Navigation bar appearance
UINavigationBar.appearance().barTintColor = UIColor (red: 231.0/255.0, green: 95.0/255.0, blue: 53.0/255.0, alpha: 0.3)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
if let barFont = UIFont (name: "AvenirNextCondensed-DemiBold", size: 22.0){
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: barFont, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
//Status bar text change to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
//Toolbar Buttons color change
UIBarButtonItem.appearance().tintColor = UIColor(red: 235.0/255.0, green: 73.0/255.0, blue: 27.0/255.0, alpha: 1.0)
UIToolbar.appearance().barTintColor = UIColor(red: 237.0/255.0, green: 240.0/255.0, blue: 243.0/255.0, alpha: 0.5)
2) I launch the app and go from main view to the connected one using push segue in the same navigation controller (this issue won't appear if I use modal segue) and unwind to the first view
3) Navigation Bar loses the title and styling, status bar also loses changed style. Though if I change it (style and title) once again in ViewWillAppear - it works fine.
The question is: should this work like that and one has to reload styling in every ViewWillAppear or am I doing something wrong?
Thanks in advance!
Michael

Resources