I have a UINavigationController and a UIViewController as its root view controller.
let rootVC = Page1ViewController() // extends UIViewController
let nav = UINavigationController(rootViewController: rootVC)
presentViewController(nav, animated: true) { () -> Void in
}
The problem is that the content of the rootVC appears under the navigation bar. I tried:
nav.navigationBar.translucent = false
this worked but I want the navigation bar to be transparent and I want the content of the rootVC not appear behind the navigation bar.
I also tried:
nav.edgesForExtendedLayout = UIRectEdge.None
but this does not change anything.
How can I get a transparent navigation bar where the content scrolls under it when scrolling but when loading the content should not appear under the navigation bar?
Remove this line:
nav.edgesForExtendedLayout = UIRectEdge.None
And add this line in your viewDidLoad from Page1ViewController
self.edgesForExtendedLayout = UIRectEdge.None
Hope it helps. Let me know if it doesn´t work and I will help you.
To address your issue of a blackbox now appearing underneath your UINavigationBar you need to set the presentation context properly. Try setting definesPresentationContext on your UINavigationController to NO. This will then use the UIWindow or parent of your UINavigationController as the presentation context.
Related
I have a UIViewController that contains a UITabBar. I want to push a new UIViewController that covers the current context (so during the animation it shows the new UIViewController covering the UITabBar). How can I do this?
I have tried using the following to push the view but this is not pushed over the UITabBar.
let vc = ViewController2()
vc.modalPresentationStyle = .overFullScreen
navigationController?.pushViewController(vc, animated: true)
I also thought that maybe I could just hide the UITabBar on ViewController2's viewWillAppear and show it on its viewWilDisappear; however, this just makes the UITabBar appear halfway through the dismissal (it looks really bad if you slowly slide to dismiss the view).
In storyboard, go to the UIViewController you are going to push.
Go to attributes inspector and check "Hide Bottom Bar on Push"
This can also be done programmatically:
let vc = viewController1()
vc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc, animated: true)
Hope it helps!
You must use presentViewController, not push.
As the title says, I cannot hide the status bar, with the regular prefersStatusBarHidden method if my UIViewController is within a UINavigationController.
If I present a UIViewController and the prefersStatusBarHidden returns true the status bar will be hidden, but if I embed the same UIViewController like the following:
let vc = VC()
let navc = UINavigationController(rootViewController: vc)
present(navc, animated: true, completion: nil)
The prefersStatusBarHidden will not be called.
Any hint on why this is happening??
Presumably this is because you are testing on an iPhone X or similar no-bezel device. It is impossible to hide the status bar when there's a navigation bar on such a device.
I created a custom search bar and embedding it in the navigation bar, it appears but after I push another view controller, the search bar does not get replaced with the title of the pushed view controller. The search bar stays persistent throughout all views, instead of getting replaced with a title. Perfect example is Instagram search tab, you search for a person and click on the cell, their profile is pushed and the search bar is replaced with the custom title, back button, etc.
First VC
self.customSearchBar.tag = 4
self.navigationController?.view.addSubview(customSearchBar)
Second VC
if let nav: UINavigationController = self.navigationController {
if let searchBar = nav.view.viewWithTag(4) {
searchBar.removeFromSuperview()
}
}
You shouldn't place the searchbar inside the navigationcontroller view as this view is the same instance on all pushed viewcontrollers.
Add the searchbar to the the depending view controllers ui.
To add a searchbar on navigationBar, this is the way.
self.navigationController?.navigationBar.addSubview(customSearchBar)
To remove it when you push it to other viewController. Write the following code in the secondVC that is pushed inside it's viewDidLoad() function. Also, set the tag of customSearchBar to any number (TAG)
if let nav: UINavigationController = self.navigationController {
let bar: UINavigationBar = nav.navigationBar
if let searchBar = bar.viewWithTag(TAG) {
searchBar.removeFromSuperview()
}
}
In the question, the customSearchBar is added to self.navigationController.view. To remove it, you can do the following:
if let nav: UINavigationController = self.navigationController {
if let searchBar = nav.view.viewWithTag(TAG) {
searchBar.removeFromSuperview()
}
}
Edit:
Adding and removing a UIViewController's view as a subview of other UIViewController
// for adding
let viewController: ViewController = ViewController()
self.addChildViewController(viewController)
self.view.addSubview(viewController.view)
viewController.view.bounds = self.view.bounds // better to use autolayout here
viewController.didMove(toParentViewController: self)
// for removing
if let vc = self.childViewControllers.last {
vc.willMove(toParentViewController: nil)
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
Issue: Searchbar shifts down when presented. Before presented, the bar is right below the navigation bar.
More Info:
The navigation bar is just a UINavigationBar that's manually added to a UIViewController through storyboard
Most importantly, the UIViewController uses UIPresentationController to create that effect where the presented VC is slightly offset from the top and the presenting VC is scaled down and "behind" the presented VC. The shift does not happen if I don't use a UIPresentationController.
searchController.hidesNavigationBarDuringPresentation = NO
Any Ideas?
self.definesPresentationContext = YES;
or
self.edgesForExtendedLayout = UIRectEdgeNone;
On a side not it is probably better to add the searchbar to the header of the tableview.
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}
if you set searchBar to tableHeaderView under iOS 11.0 will cause this question, so you can do it like this. everything will be ok!
I have a tab-bar based app and I want to add Navigation Bar at the top of the app. Please note that I am using a library called PageMenu that creates 2 TableViews inside 1 parent ViewController.
What I tried was, adding a new ViewController and Editor->Embed in Navigation Bar. Place it before Tab Bar Controller, ctrl+drag to Tab Bar Controller to set the relationship of root view. Finally set Nav Bar Controller as initial view controller. But this fails like this:
(Top became pretty weird, blurry and the sub-header of PageMenu got disappeared. Maybe it's under that blurry thing because I can still swipe between 2 table views.
Secondly, I tried removing the Navigation Controller, and add Navigation Bar to the ViewControllers manually. This worked for table view and view controllers but not the PageMenu one. When I tried it on PageMenu Controller, it didn't show any navigation bar.
Please note that, in the Demo, they used Navigation Bar as Parent and sub-TableViews, and they achieved Navigation Bar with this as well as Storyboard > Navigation Controller:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "HEADER"
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
Lastly, I tried..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav1 = UINavigationController()
let first = ViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [first]
let second = SecondViewController(nibName: "SecondViewController", bundle: nil)
let nav2 = UINavigationController()
nav2.viewControllers = [second]
let tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2]
self.window!.rootViewController = tabs;
self.window?.makeKeyAndVisible();
return true
}
But the result I get is:
What I want to achieve (but with TabBarController; NavBar just for header):
What I have now is this. I just want to add NavigationBar at the top of it like the above PageMenu example
Update:
Lasly, I also tried:
But same issue:
You can also create like wise story board that helps to solve your problem.
Here I can created sample code what you want no single line code change but changes into storyboard only.
Download source code from here.
Not add Editor->Embed in Navigation Bar before Tab Bar Controller
add Editor->Embed in Navigation Bar before of View controller which you connect from Tab Bar controller.
Because its work for particular Tab Vise so we have to add Editor->Embed in Navigation Bar before of View controller