I want to make my tabbar half transparent by setting the translucent value to true. However it is not doing the trick. I have a TabBarVC assigned to the tab bar with the following code. The tab bar remains solid
class TabBarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// color of background -> This works
self.tabBar.barTintColor = UIColor.purpleColor()
// color when selected -> This works
self.tabBar.tintColor = UIColor.redColor()
// This does not work
self.tabBar.translucent = true
}
I also tried to do something like
UIColor(red: 246.0/255, green: 246.0/255, blue: 246.0/255, alpha: 0.5)
But it does not seem to work. I did a bit of search on Google but everyones issue seems to differ from mine. Could anyone help me out here?
Thanks,
What you're doing is most likely working to adjust the transparency of the tab bar. However, you need to set the corresponding view controllers to be "Under Bottom Bar" in the IB.
Related
I have a tableViewController as my root view controller.
I've been trying to change the color of the status bar to match something like this:
However, when I set:
navigationController?.navigationBar.prefersLargeTitles = true
And apply:
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.blue]
}
The background color doesn't change at all.
Only when I begin SCROLLING and the navigation bar collapses, do I get a color.
Has anyone else ran into this issue before? I built another viewController to test out "PrefersLargeTitles" WITHOUT a tableview scroll feature. And there was no background color either.
Is there anyway to keep the tab bar color exactly the same regardless of the color of the view in the view controllers? The bottom picture with dashboard selected is darker than the top one because the view.backGroundcolor = .lightGray in the view controller.
I tried setting the view.bottomAnchor equal to the view.safeAreaLayoutGuide.bottomAnchor, but even then if the view is set as light gray, the tab bar will be slightly darker than the view controllers that have a white background.
I also set the self.tabBar.barTintColor = .white
and self.tabBar.alpha = 1.0
It's not just a perception thing either as I checked the exact color in hexcode.
Relevant lines of code :
final class TabBarViewController: UITabBarController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBar.barTintColor = .white
self.tabBar.alpha = 1.0
}
}
Should this code go in the init instead that sets up the corresponding view controllers?
The color you are setting for the tab bar is only a tint. The only way to get absolute control over the color is to make a resizable UIImage of the desired color and set the tab bar's backgroundImage property.
https://developer.apple.com/documentation/uikit/uitabbar/1623469-backgroundimage
I have an odd situation where I cannot for the life of me get an app to start with the UINavigationBar set to transparent black. The app consists of a UINavigationController with a UIPageViewController as root. These are loaded from a storyboard when the app launches.
The app runs and the UINavigationBar shows up as opaque. As soon as I start dragging the UIPageViewController it triggers some sort of redraw and the UINavigationBar becomes transparent as desired.
I tried a few different ways of setting the UINavigationBar to transparent black, and none of them produced behavior any different than described:
I set these keys in Info.plist:
<key>UIStatusBarTintParameters</key>
<dict>
<key>UINavigationBar</key>
<dict>
<key>Style</key>
<string>UIBarStyleBlack</string>
<key>Translucent</key>
<true/>
</dict>
</dict>
I set the UINavigationBar appearance in the AppDelegate in didFinishLaunchingWithOptions:
UINavigationBar.appearance().tintColor = UIColor.black
UINavigationBar.appearance().isTranslucent = true
I subclassed UINavigationController and set NavigationBar properties in viewDidLoad():
navigationBar.barStyle = .black
navigationBar.isTranslucent = true
navigationBar.setNeedsDisplay()
I set the NavigationBar properties in viewDidLoad() of the UIPageViewController (the navigation controller's root view controller):
navigationController!.navigationBar.barStyle = .black
navigationController!.navigationBar.isTranslucent = true
navigationController!.navigationBar.setNeedsDisplay()
I also tried toggling the navigation bar on / off to trigger a refresh
navigationController!.isNavigationBarHidden = true
navigationController!.isNavigationBarHidden = false
For the sake of completion, I set the navigation bar properties in viewDidLoad() of the child view controller of the UIPageViewController.
In every single case I get the same behavior: opaque bar until I touch the screen and start a drag, at which point it switches to transparent.
What's odd is that setting other attributes of the navigation bar, such as titleTextAttributes, leftButtonItem, and rightButtonItem works just fine and the changes are reflected immediately.
I had a similar requirement with Swift3 an achieved it in this way:
In your view controller which contains PageViewController add the following code in viewDidLoad()
func makeNavigationTranslucent() -> Void {
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.backgroundColor = UIColor.init(colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 0.9)
}
Note : Change navigationBar.backgroundColor to the one which you want.
In your storyboard select your controller and under attribute inspector with simulated metrics heading set top bar to Navigation Translucent Bar. Refer below image :
This is kind of a hacky solution, but I managed to get it working by hiding the default navigation bar and adding a new one over it. In the page view controller viewDidLoad() function:
navigationController!.isNavigationBarHidden = true
let newNavBar = UINavigationBar()
newNavBar.barStyle = .black
newNavBar.isTranslucent = true
newNavBar.titleTextAttributes = [NSFontAttributeName: UIFont.mainFontThin(24),
NSForegroundColorAttributeName: UIColor.white]
// etc...
navigationController!.view.addSubview(newNavBar)
newNavBar.frame = CGRect(
x:0, y:0,
width:navigationController!.view.bounds.width,
height:navigationController!.navigationBar.frame.height + UIApplication.shared.statusBarFrame.height
)
newNavBar.pushItem(navigationItem, animated: false)
This shows the translucent navigation bar like we want, but there's still a small problem where the app status bar is black under the translucent nav bar. As before, once you drag a finger the status bar updates to transparent. I'm not sure how to fix this, adding a call to setNeedsStatusBarUpdate() didn't help. So this is a partial fix.
I am trying to change the color of my navigation bar in a view which contains a UISearchController. I have previously set the color of my navigation bars for the whole app in my appDelegate, but i want this views nav bar to have a different color. The issue is that i dont know which function to place the code such that it will override the appDelegate code. For example, viewDidLoad and viewWilAppear do not change the color when the view first loads, only after i enter and cancel the searchController. Which function should i place the following?
UINavigationBar.appearance().backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
There are two ways to do this. You can either modify the appearance proxy that you have set up in the AppDelegate for the whole app, or you can modify the individual navigationbar for the particular screen.
When you dismiss the view - you need to reset the barTintColor in the viewWillDisappear.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Set to new colour for whole app
UINavigationBar.appearance().barTintColor = UIColor.blueColor()
// Or, Set to new colour for just this navigation bar
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
//Revert to old colour, whole app
UINavigationBar.appearance().barTintColor = UIColor.redColor()
//Revert to old colour, just this navigation bar
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
}
Instead of changing the navigation bars color directly, you should change the navigation controller's bar color in viewWillAppear and change it back when you dismiss the view.
I have an app with multiple views and controllers but on just one of those views I would like to make the top navigation bar transparent with white text. I have the following code in the controller for said view:
override func viewDidLoad() {
super.viewDidLoad()
let bar:UINavigationBar! = self.navigationController?.navigationBar
bar.tintColor = UIColor.whiteColor()
bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
bar.shadowImage = UIImage()
bar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
}
When I open the app the top bar on the other views looks as expected and when I open this view the top bar looks like I want it to. But when I navigate from the this view to the others, the other views inherit the changes made by the above code to the top bar.
Is there a way to prevent this so that the navigation bar only changes for that particular view while leaving the rest intact?
Thanks in advance!
There is only one navigation bar for any particular navigation controller, so if you change it in one controller, it will be changed for all. The way to fix that is to change it back to whatever you want for the other controllers in viewWillDisappear (or viewDidDisappear). You might also need to move the code you show to view willAppear if you are coming back to this same instance when another controller gets popped.