I am using WKWebview to show my content. When I scroll webview, I would like to hide my navbar. I use below code and nothing is happen.
navigationController?.hidesBarsOnSwipe = true
Can someone tell me, how to do this.
Add top constraint for the webView with superview, set hidesBarsOnSwipe to true, and then load the URL (check screenshot for reference):
One of the ways through code is you can use the scrollview delegate method to identify if webview is scrolled. And there you can use navigationController?.setNavigationBarHidden(isHidden, animated: true).
Something like this:
set scrollview delegate in viewdidload self.webView.scrollView.delegate = self. And then use
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0
{
print("up")
navigationController?.setNavigationBarHidden(false, animated: true)
}
else
{
print("down")
navigationController?.setNavigationBarHidden(true, animated: true)
}
}
Related
I tried implementing the simple one line of code shown below into my viewDidLoad in order to have my navigaion bar hide when a user swipes on my collectionview. However the action only works when i swipe from my navigation bar, no where else. I would like it to hide on swipe from my collection view which of course takes up most of the view.
A mini video of the issue
override func viewDidLoad() {
navigationController?.hidesBarsOnSwipe = true
}
Is the view controller embedded in the navigation controller?
Swift 4, Swift 5:
Hide your navigation bar when you scroll upside and similarly show when you scroll downside.
scrollViewDidScroll() method will get called every time when you scroll your CollectionView, TableView, ScrollView etc.
Try this code:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Check the scroll direction here
if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0) {
print("Show")
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
}
else {
print("Hide")
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
}
}
I have created a parallax type scroll effect (or stretchy header) in my view controller.
I have a uiscrollview, which is anchored to the windows margins. Inside of this I have a view, that is anchored with 0 constant constraints to the scroll view, and set to equal widths.
Inside that view is the content. At the top of the content is an image, and this is anchored to the top of the window (safe area guide) using two constraints.
First is constant=0 priority=750.
Second is constant<=0 priority=1000.
This works.. when i scroll up everything scrolls up. When i scroll down from the top, the image stretches and then bounces back.
Question: I am trying to also implement the navigation bar to hide when user scrolls. I have added this code to the view controller:
override func viewDidAppear(_ animated: Bool) {
navigationController?.hidesBarsOnSwipe = true
}
This works ok when I scroll up (the nav bar fades and animates up until hidden), but when i scroll back down the navigation bar doesn't return. I assume it is hidden somehow by the image that is anchored to the top of the window. But how can I adjust/reattach the navigation bar?
xcode 9 - swift 4
Try with below code might be help.
Make sure you should take delegate of UIScrollView and implement scrollViewDidScroll delegate method.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollOffset: Float = Float(scrollView.contentOffset.y)
if scrollOffset < 0 {
navigationController?.hidesBarsOnSwipe = false
navigationController?.setNavigationBarHidden(false, animated: true)
}
else {
navigationController?.hidesBarsOnSwipe = true
}
}
I have NavigationController that handles navigation through my app.
According to my design, the very first view should have no visible NavigationBar. All the others after, will.
In this FirstView, I'm using this so far to hide the NavBar, inside the ViewDidLoad:
self.navigationController?.isNavigationBarHidden = true
From this FirstView I can access other Views. In these other views I show the NavBar using:
self.navigationController?.isNavigationBarHidden = false
My problem is that:
When I navigate from a View with Visible NavBar, back to the FirstView with the Hidden NavBar, the NavBar is now visible.
Basically the NavBar only hides the very first time then shows if I use the back button.
How Can I Prevent this ?
Thank you!
Move that code to viewWillAppear() instead of viewDidLoad().
viewDidLoad() is only called once per instantiated view controller, whereas viewWillAppear() is called whenever the view controller is about to be presented on screen.
You can read more about the view controller lifecycle here.
Write below code in your FirstViewController's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = true
}
And in your SecondViewController's viewWillAppear method write below code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}
Do not try to hide and show nav bar in viewWillAppear and viewWillDisappear subsequetly in FirstViewController.
You can use this function to hide NavigationBar with cool animation:
func setupAnimationForNavigationBar(caseOfFunction: Bool) {
if caseOfFunction == true {
UIView.animate(withDuration: 0.5) {
self.navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: -200)
}
} else {
UIView.animate(withDuration: 0.5, animations: {
self.navigationController?.navigationBar.transform = CGAffineTransform.identity
})
}
}
If you want to hide NavigationBar, so set it "True" and if you want to call NavigationBar again, set it "False"
I have one tableview that cut off in the last row of the cell. But when i click one cell, and click back from the next viewcontroller, my tableview is correctly shown all the cell.
Whast wrong with my tableview?
I set my tableview like this in home controller:
in ViewDidLoad:
self.automaticallyAdjustsScrollViewInsets = false
override func viewDidLayoutSubviews() {
self.myTableView.frame = self.view.bounds
}
and present the next view controller like this:
self.navigationController?.pushViewController(nextVC, animated: true)
and navigate back like this:
#IBAction func back(){
self.navigationController?.popViewControllerAnimated(true)
}
use autolayout. set top,bottom,leading and trailing constraints to it. and don't set frame of tableview like self.view.bounds
Try this code in viewWillAppear Method..
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated) // No need for semicolon
self.myTableView.frame = self.view.bounds
}
I'm trying to add TopView in my application, it will be the same for each views. I do it like this
let vcTopMenu = storyboard?.instantiateViewControllerWithIdentifier("TopMenu")
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.rootViewController = vcTopMenu
win.makeKeyAndVisible()
But when I add other viewControllers (I do it transparent) I can see buttons of TopView, but I can't click on it. It's a code from TopView
override func viewDidLoad()
{
super.viewDidLoad()
print("loaded")
}
#IBAction func btn(sender: AnyObject)
{
print("do something")
}
I see "loaded", but clicking doesn't work, how can I click through view? Thanks!
If I understand your question correctly, you're placing a translucent/transparent UIView on top of another UIView with a button you want to press?
The topmost UIView by default receives the touches. More on this here.
It's not a very standard/practical way to do things, but if you absolutely must, check out this answer: https://stackoverflow.com/a/4010809/4396258