Transclucent navigation bar moving section header - ios

I am just a simple guy struggling to get a translucent navigation bar.
I have tried to set it in appDelegate and in the spesific ViewController.
What i end up with is this:
Does anyone know why the section header in white is moved down? The navigation bar is not even translucent when i scroll and i canĀ“t set a color to it. (i can set color when translucent = false)
I have tried to set it to translucent and opaque and inferred in storyboard.(not working)
Update:
let colorImage = imageFromColor(UIColor(red:0.22, green:0.23, blue:0.29, alpha:0.5), frame: CGRectMake(0, 0, 340, 64))
self.navigationController!.navigationBar.setBackgroundImage(colorImage, forBarMetrics: UIBarMetrics.Default)
self.navigationController!.navigationBar.shadowImage = colorImage
self.navigationController!.navigationBar.translucent = true
The colorImage is a image i make with a rgb-uicolor. The outcome is this: navigation bar 2
It is transclucent between the section header, but the top bar is not!
UPDATE 2

If you are trying to set a translucent background for navigationBar across your application, you can use the following code in application:didFinishLaunchingWithOptions method of your AppDelegate
let colorImage = getImageWithColor(UIColor(red:0.22, green:0.23, blue:0.29, alpha:0.5), size: CGSizeMake(360, 64))
UINavigationBar.appearance().setBackgroundImage(colorImage, forBarMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().translucent = true
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
Result Screenshot:

Related

AppStore NavigationBar iOS 13

How is it possible to customize the navigation bar like in the AppStore app?
I already tried to set the following (it is a subclass of UINavigationController):
self.navigationBar.setBackgroundImage(UIImage.with(color: UIColor.red), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
self.view.backgroundColor = .blue
Then the large navigationBar is blue and the small/collapsed navigationBar is red. But what I have to adjust that I get a translucent navigationBar when the navigationBar is collapsed?
[

How to set a view's color to match a translucent navigation bar's color?

I need to increase the height of the navigation bar. For this, I have added a view (of desired height) under the navigation bar. The navigation bar is set to be translucent. So the nav bar color is rendered slightly different than the actual hex value. Now I need to match the navigation bar's color to the view below. Following is the code that I am using.
func setupNavigationBar() {
title = "Profile"
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage(named: "")
headerView.backgroundColor = navigationController?.navigationBar.barTintColor
headerView.isOpaque = false
}
I am getting different shades as below.
How can i make the view's color to be the same as navigation bar's color? I can get near to the desired color by reducing the view's alpha but I am doubtful about that approach since there is no standard defined regarding it.
P.S. The navigation bar has to stay translucent.
You just need to set opacity of headerView to 0.85
headerView.backgroundColor = navigationController?.navigationBar.barTintColor
headerView.layer.opacity = 0.85
headerView.isOpaque = false
You can download the sample code from here:
Please ignore other unused code in the sample code.
Explanation:
When you're set navigationController style as translucent then the system automatically take layer opacity 0.85 for UINavigationController
I personally check it by iterating all subview of UINavigationController.
Set the background colour to clear for the navigation bar using :
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
Hope this helps.
You can set the background color to nil and it will follow the background color.
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = nil
navigationController?.navigationBar.standardAppearance =
appearance
navigationController?.navigationBar.compactAppearance = appearance

Changing alpha value of navigationbar so underlying objects are visible

Hi I want to implement a transcluent NavigationBar so underlying objects are visible.
It should look like here:
This view is a custom view wich uses alpha value to achieve the translucency behavior.
I tried this to implement my custom NavigationBar like this:
UINavigationBar.appearance().barTintColor = .white
UINavigationBar.appearance().tintColor = UIColor(netHex: CxtColor.black.rawValue)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().inputView?.alpha = 0.4
UINavigationBar.appearance().alpha = 0.4
But its not translucent and also the changing of the alpha value has no really effect on the NavigationBar. Its just a white navigationbar.
I don't want to create a full transparent navigationbar.
]you can access navigationBackgroundView
self.navigationController?.navigationBar.isTranslucent = true
let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
navigationBackgroundView?.alpha = 0.3

Black Translucent Navigation Bar Opaque Until First Drag

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.

Using custom image as background of Navigation bar of UINavigationController

I am trying to use an image as the background for my navigation bar. I can get the image into the bar, but it doesn't appear properly. I have attached an image of the desired background, and the result that I am getting.
In addition this is the code I am using to get the image into the bar background.
navigationController.navigationBar.setBackgroundImage(UIImage(named:"Top Bar Slice"), forBarMetrics: UIBarMetrics.Default)
EDIT:
One of the answers got the image into the code, but now it is tile-ing across the bar. Is there a way to fix this?
I am now using this code in the app delegate:
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "Top Bar"), forBarMetrics: UIBarMetrics.Default)
You need to override UINavigationBar.appearance()
Option 1:
let image = UIImage(named: "yourImage")
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default)
Option 2:
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "yourimage"), forBarMetrics: UIBarMetrics.Default)
You can also add a titleView to the navigation item in your viewController.
Option 3:
navigationItem.titleView = UIImageView(image: UIImage(named: "yourimage"))

Resources