Navigation bar item as tab bar item - ios

I did search something similar with my question, but didn't find anything to help me solve my issue.
Is it possible to set behavior for navigation bar item as tab bar item?
I use TabBarController. Each tab item should show the corresponding ViewController. When user tap on the item in nav bar it should show the corresponding ViewController too with the bottom tab bar. And this ViewController should be as tab bar controller, but not display tab item in the bottom tab bar.
How I can achieve the scenario describe above? Or any alternative idea how it possible to implement, please! Almost always the top and bottom bars should be visible for each ViewController.

Drag and drop from your bar button item in IB to your class file and define an outlet, then in viewDidLoad create a gesture recognizer, add an action to your gesture recognizer and you are all set. I hope it helps.
class SomeClass: UIViewControler {
let someBarButton:UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(SomeClass.tapFunction))
someBarButton.addGestureRecognizer(tap)
}
func tapFunction() {
//take me to a view controller
self.performSegue(withIdentifier: "myIdentifier", sender: self)
}
}

Related

Tab bar item as button

I have 5 tab bar items in my tab bar, 4 of which have segues to navigation controllers which lead to view controllers. I want to make the middle tab bar item act as a button, so that when I click on it, I have control over what happens.
Currently, my middle tab bar item is also connected to a navigation controller, which is not right because now when I click the tab bar item, it opens a black navigation controller. How can I convert the middle tab bar item to act as a button, rather than going to a navigation controller?
If I remove the navigation controller, it also removes the tab bar item from the tab bar.
If you want your tab bar item to act as a button you could subclass a UITabBarController and implement this delegate function:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// Check if bar item selected is center
if viewController.tabBarItem.tag == 2 {
// Do Something Here ...
// Present View Controller
guard let navigationController = storyboard?.instantiateViewController(withIdentifier: "NavigationController") as? UINavigationController else { return false }
present(navigationController, animated: true)
// Returning false will not open the connected tab bar item view controller you attached to it
return false
}
// Return true to open the connected tab bar item view controller you attached to it (e.x. everything but the center item)
return true
}
To implement a custom tab bar and use tab bar items like a normal button
Add a new Tab Bar view to your view controller (VC)
Add the custom tab bar items you need and assign tag numbers on them (On Attribute inspector)
Add a delegate outlet from Tab bar View to your view controller (Right Click and drag To VC)
On your view controller, subclass UITabBarDelegate, like this
class ViewController: UIViewController, UITaBarDelegate {}
Implement this delegate function, then it should works
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 1 {
//Do something
}
if item.tag == 2 {
//Do something
}
.......
}

Navigation bar specific to each view controller like Apple Music

How do I have the navigation bar specific to each view controller? On the right side, the navigation bar stays with the view controller, and the left view controller has its own navigation controller. Should I just make a custom transition? Any ideas ?
Add this code in your first viewController
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(false, animated: false)
}
Then add navigationBar in your viewcontrollers from storyboad. Or you can add one in viewDidLoad programmatically

Center TabBar button going to back

I have created a centre TabBar button by subclassing UITabBarController and adding a bigger button to the centre.
Now when i go to an item in my menu, which is just a static TableViewController embedded in a ViewController the button will not hide with the TabBar, where i have just set the bottom bar to hide on push within interface builder.
Static Table View
Button Still Shown when menu item page loaded. This is just an empty ViewController with a 'show' segue from the static TableViewCell
Hid Bottom Bar Selected.
i also have a problem when going back to the menu and the TabBar is displayed again. The button is now behind the TabBar.
------EDIT-------
I revised my outlook and have added the following to my TabBarController class
override func viewDidLayoutSubviews() {
if self.homeButton != nil {
self.view.bringSubview(toFront: self.homeButton)
for test in self.view.subviews {
if let subView = test as? UITabBar {
if subView.isHidden == true {
self.homeButton.isHidden = true
} else {
self.homeButton.isHidden = false
}
}
}
}
}
its just a little slow, there is a delay of about 1 second from when the view changes to when the button is wither hidden or brought to the front.
Any better ideas about how to display this faster?

iOS UINavigationController barHideOnTapGestureRecognizer and UIButton interference

I have developed an app that makes use of the iOS8 feature to show or hide the navigation bar on a tap of the view.
However, the main view contains a UIButton which also act upon taps. The problem is that both 'objects' are receiving the tap and if I tap the button, the navigation bar toggles its visibility.
I can get to the barHideOnTapGestureRecognizer via the navigation controller but not really sure what can be done with it to stop it responding if a button is tapped.
Is there a way (apart from switching off or changing to 'Swipe to Hide') to subdue the navigation bar's appearance/disappearance when a button is pressed?
Don't use the standard barHideOnTapGestureRecognizer. Fortunately, it's not hard to roll your own:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "toggleBarsOnTap:")
self.view.addGestureRecognizer(gestureRecognizer)
}
func toggleBarsOnTap(sender: AnyObject?) {
let hidden = !self.navigationBarHidden
self.setNavigationBarHidden(hidden, animated: true)
self.setToolbarHidden(hidden, animated: true)
}
Taps on the view will show/hide the bars, and taps on controls (subviews of the view) will not.
[self.navigationController setNavigationBarHidden:YES];

Hide Bars On Tap with only a bottom toolbar

When you drag a UINavigationController into a storyboard, you can enable the Hide Bars On Tap option which will hide/show the navigation bar and toolbar upon tapping anywhere in the UIView. This works well except in the case where you don't have a navigation bar and you only have a toolbar. (You checked Shows Toolbar but not Shows Navigation Bar.) When you launch the app there is no visible navigation bar but when you tap a navigation bar slides down from the top, then if you tap again both bars slide away.
How can you use Hide Bars On Tap with only a toolbar and prevent a navigation bar from appearing?
You can get hold of the gesture recognizer via the barHideOnTapGestureRecognizer property.Then you add actions to this gestureRecognizer to do whatever you want.
In your case ,this snippet will work:`
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.hidesBarsOnTap = true
navigationController?.barHideOnTapGestureRecognizer.addTarget(self, action: "tap:")}
func tap(gest: UIGestureRecognizer){
navigationController?.navigationBar.hidden = true
}

Resources