NavigationBar Translucent Animation - swift

I have a ViewController which starts with a translucent navigationbar with clear background
navigationBar.isTranslucent = true
navigationBar.barTintColor = .clear
I have scrollView as the first child and scrollView's contentInsetAdjustmentBehavior is set to .never. Now when I scroll the view and scrollView.contentOffset.y > 200 I decided to change the navigationbar as
navigationBar.isTranslucent = false
navigationBar.barTintColor = .someColor
the scroll view get pushed down. I understand that when the view is supposed to be below the navigation bar when it is not translucent. But this makes my scroll effect look ugly. I did little research and found that I can enable Under the top bar option from storyboard, nut unfortunately I am building my using swift, and not using storyboard. I also tried using edgesForExtendedLayout = [.top] but did not work form me.
I wanted was a smooth scrolling behaviour while scrolling, with Navigationbar changing the property of translucent with respect to scrolling contentOffset.

Related

How to make transparent navigate bar with Table View Cell

I faced with the Table View Cells issue. If I choose default View Controller settings, cells are located under transparent navigation Bar. This is unacceptable because bars items overlap the cell.
navigation bar overload the tableView
If I changing Extend Edges setting at the tab on the right side, specifically "Under Top Bars", cells as expected are located under bar, but in this case NavigateBar is black.
navigation Bar is black
As you see I tried to fix that issue with the Simulated Metrics, but nothing is chanced. Also self.navigationController?.navigationBar.isTranslucent = true and self.navigationController?.view.backgroundColor = .clear in viewDidLoad method did't give any result. I able to color navigation Bar, but i need it transparent. Any ideas?
I guess you haven't set a background color to the ViewController's view. That's why it's appearing black.
Try adding the background color you want and it should work as expected.

UINavigationController weird transparency behaviour when presenting

I'm experiencing a weird glitch with the transparent navigation controller. When presenting a view controller with a UIImage at the top of the navigation controller: at first presents half of its background blur with a dark section, and half with a clear one; and after a very short moment it changes for a full dark background. As shown in the gif:
The UIImage displayed is mostly white, the borders are very close to pure white. So the grey color does not make much sense (less sense makes the rapid change).
I did disable extend edges under top bar in the Storyboard for the presented view controller. So the image is not hidden behind the Navigation Controller. With this option enabled the glitch does not appear, but I don't want to hide part of the image.
Disabling the transparency, solves my problem, but I'd like to be able to keep the transparency effect.
Edit: I did notice the "grey effect" also makes the navigation controller opaque. But only for that view, when going back, is transparent again.
Xcode 11, Swift 5, iOS 13.2, iPhone XS.
Thank you very much for your help.
I finally found the problem.
I didn't want the UIScrollView to bounce on top so I used this code:
extension ProblematicViewController: UIScrollViewDelegate {
/// Prevent bounce at top
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
scrollView.contentOffset.y = 0
}
}
}
Which meant the ViewController's Adjust Scroll View Insets were overwrite (I think). So my scroll view did lay under the UINavigationController, to prevent this from happening, I did disable Extend Edges: Under Top Bars, so the root UIView containing the UIScrollView was not covered by the UINavigationController. So the transparency effect could never happen because the view was never under the top bar. More about Adjust Scroll View Insets on this post (helped me to find the problem).
Removing those lines and enabling Adjust Scroll View Insets and Extend Edges: Under Top Bars, solves the glitch problem, but the UIViewController does not have the "prevent bounce on top" behaviour that I wanted.
This kinda explains the weird behaviour of the UINavigationController because the SO has to calculate a transparency over nothing, but is sill strange, the transparency effect is calculated when the ProblematicViewController is fully presented, and not on viewDidLoad() or another view's life cycle before is shown to the user, so the transition is smooth, even when nothing lays behind the effect.

push transition with searchController not animating navigationBar change in size smoothly in some cases

I am using largeTitles and a searchController. when I animate a push transition, the searchBar sometimes does not animate it's disappearance. The space for the searchBar suddenly disappears after the push causing a rough transition animation.
I thought the solution was to set the searchController.definesPresentationContext = true but that is not working. Wondering if there is another fix for this.
You need to change you code to definesPresentationContext = true on the view controller itself.

Display navigationbar when scrolling down

On the top of the ViewController, I have a image and I hide the navigationbar, for a better visual effect.
If the user scrolls up, there is a zoom on the image. No problem so far.
If the user scrolls down, I want to display the navigation bar with animation (very light to the correct background color of the navbar)
I ve checked here a good tutorial with the new possibilities with Ios8.
In fact, i need to perform the opposite of hidesBarsOnSwipe
So firstly, to hide the navigationbar I need to
self.navigationController?.isNavigationBarHidden = true
And after some search, I think I will need to use UIScrollViewDelegate.
But I have no idea how could i implement it .
Any hint?
What you have to do is to implement the UIScrollViewDelegate and more precisely the scrollViewDidScroll(_:) method (see documentation). This method is called each time the scroll view is scrolled.
So, in this method, you have to check that the user scrolled down and then hide the navigation bar by calling the setNavigationBarHidden(_:animated:) method of your current navigation controller (see documentation)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let defaultOffset = view.safeAreaInsets.top
let offset = scrollView.contentOffset.y + defaultOffset
navigationController?.navigationBar.transform = .init(translationX: 0, y: min(0, -offset))
}
use this function and it scrolls up the navigation bar while scrolling up and whenever you scroll down then the navigation bar appears again..

Setting NavigationController's NavigationBar translucent property to false causes extra padding

There appears to be extra padding at the top of the root content view when the translucent = false property is set on the NavigationController's NavigationBar (programatically or via the StoryBoard).
I've tried adjusting the scroll view insets but to no avail. translucent = true doesn't cause this problem.
Any ideas why this is happening and what the easiest way to resolve it is?
Ah-ha - got there in the end. When not using a translucent navigation bar you need to ensure that both adjusts scroll view insets and extend edges under opaque bars are set to true on all your view controllers.
Here are the StoryBoard settings: