I have problem with UITabBar. I need to make a custom action for Item (UITabBarItem). What do I need to add to make it working?
import UIKit
class ViewController: UIViewController {
#IBOutlet var TabBar: UITabBarItem!
#IBOutlet var Item: UITabBarItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBarHidden
self.navigationItem.hidesBackButton = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {
var selectedTag = tabBar.selectedItem?.tag
println(selectedTag)
if selectedTag == 0
{
}
else
{
}
}
}
In each ViewController place this function:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//set inital view
}
Then put your code to execute in here and when the view appears it will execute.
Okay then, what I think you want then is not a UITabBar but instead a UIToolBar. From Apple:
"A tab bar is a control, usually appearing across the bottom of the screen in the context of a tab bar controller, for giving the user one-tap, modal access to a set of views in an app. Each button in a tab bar is called a tab bar item and is an instance of the UITabBarItem class. If you instead want to give the user a bar of buttons that each perform an action, use a UIToolbar object."
For the UIToolBar description see:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIToolbar_Class/index.html#//apple_ref/occ/cl/UIToolbar
Related
I followed this tutorial to smoothly hide the statusBar smoothly hide statusBar and everything works fine when I use it on practice projects. I use the code in other project's that do not have SplitVC but have a tabBar and uses a navVC & tableView and everything works fine. In those I can successfully make it appear/disappear.
In my actual project I'm using a SplitViewController for iPad. I noticed when I implemented the directions from the link to my SplitViewController the statusBar wouldn't hide. I then made a new project using Apple's default MasterDetailApp to make sure I wasn't doing anything wrong but it doesn't work there either. I kept all of Apple's original code and only added in the necessary methods to make the statusBar appear/disappear
in info.plist I added the View controller-based status bar appearance and set it to YES
in storyboard I added a purple button to the DetailVC to trigger the statusBar disappearance. I also added in the method to make the backBar button disappear/reappear
I added all the methods to make the statusBar disappear/disappear to the DetailVC scene.
I added a tapGesture to the scene to make the statusBar and backButton reappear
I clicked the Plus button on the Master scene, a date appeared, clicked it to get to the DetailVC, pressed the purple buttonPressed to hide the statusBar and backButton but only the backButton gets hidden. I touch the background and the backButton reappears. The statusBar doesn't move.
I kept all the original code from Apple's project's and added mines below it:
class DetailViewController: UIViewController {
//MARK:- Apple's code
#IBOutlet weak var detailDescriptionLabel: UILabel!
func configureView() {
if let detail = detailItem {
if let label = detailDescriptionLabel {
label.text = detail.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
// make backButton and statusBar reappear when scene is tapped
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(showBackButtonAndStatusBar))
view.addGestureRecognizer(tapGesture)
}
var detailItem: NSDate? {
didSet {
configureView()
}
}
//MARK:- Outside of the tapGesture in viewDidLoad everything below here is what I added
// bool to determine wether to hide the statusBar or not
var statusBarShouldBeHidden = false
// api method to allow the staus bar to be hidden
override var prefersStatusBarHidden: Bool{
return statusBarShouldBeHidden
}
// api method to animate status bar appearance/disappearance
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
return .slide
}
#IBAction func buttonTapped(_ sender: UIButton) {
// 1. hide backBar button
navigationItem.setHidesBackButton(true, animated: false)
// 2. set bool to true
statusBarShouldBeHidden = true
UIView.animate(withDuration: 0.25){
// 3. api method to allow the statusBar to disappear
self.setNeedsStatusBarAppearanceUpdate()
}
}
//called when background is touched and added to tapGesture in viewDidLoad
#objc func showBackButtonAndStatusBar(){
// 1. set bool to false
statusBarShouldBeHidden = false
UIView.animate(withDuration: 0.25){
// 2. bring statusBar back
self.setNeedsStatusBarAppearanceUpdate()
}
// 3. bring backButton back
navigationItem.setHidesBackButton(false, animated: true)
}
}
How can I get the SplitViewVC to let me hide the statusBar?
It appears that you are trying to hide the status bar through the detail view controller. The status bar in the user interface is controlled only by the split view controller because it is on top of the view controller hierarchy. Therefore, the easiest way to control the behavior of the status bar is to subclass UISplitViewController and then override the prefersStatusBarHidden computed property in the subclass. Also, make sure you go to your storyboard and change the split view controller's custom class field in the Identity inspector to your subclass.
---Updated Answer---
#LanceSamaria Okay, I took your code above and tweaked some things. First of all, I only added the button action and not the tap gesture. Also, I commented out the hiding the back button, because this is important in the UI in order to be able to go back to the master view. Anyway, now when you click the button, the SplitViewController will hide the status bar. If you click the button again, then the status bar will reappear.
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var detailDescriptionLabel: UILabel!
var statusBarShouldBeHidden = false
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
if let label = self.detailDescriptionLabel {
label.text = detail.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
/* override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
return .slide
} */
var detailItem: NSDate? {
didSet {
// Update the view.
self.configureView()
}
}
#IBAction func buttonTapped(_ sender: UIButton) {
// 1. hide backBar button
//navigationItem.setHidesBackButton(true, animated: false)
// 2. set bool to true
statusBarShouldBeHidden = !statusBarShouldBeHidden
UIView.animate(withDuration: 0.25){
// 3. api method to allow the statusBar to disappear
guard let svc = self.splitViewController as? SplitViewController else { return }
svc.statusBarShouldBeHidden = self.statusBarShouldBeHidden
svc.setNeedsStatusBarAppearanceUpdate()
}
}
}
Also, one more really important thing. Below is my code for the split view controller subclass. Note that I use the same variable name "statusBarShouldBeHidden" in both the split view controller and the detail controller.
import UIKit
class SplitViewController: UISplitViewController {
var statusBarShouldBeHidden = false
override func viewDidLoad() {
super.viewDidLoad()
}
override var prefersStatusBarHidden: Bool {
return statusBarShouldBeHidden
}
}
Thank you for posting this question. This has helped my learn a lot trying to solve this problem. Please let me know if you still have a question about what this.
Or is the easy way to implement the search bar manually on all needed views the correct way?
I have to add a searchbar on nearly every view in my app (in addition to add a button on every views navigationbar). But I am not sure what is the best approach to achieve this target.
Should I subclass a navigationbar or the whole navigation controller?
Or is the easy way to implement the search bar manually on all needed views the correct way?
If I should subclass which class is the right one?
My Idea is to subclass the UINavigationController, add a UISearchBar and after the search results are fetched to open a UITableViewController with the search results.
This is my current approach (without implementing the searchbar delegate just to check if I am on a working solution)
import UIKit
class MyNavigationControllerViewController: UINavigationController {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.backgroundColor = UIColor.red
self.createSearchBar()
}
func createSearchBar() {
let searchBar = UISearchBar()
searchBar.showsCancelButton = true
searchBar.placeholder = "Search"
// searchBar.delegate = self
self.navigationItem.titleView = searchBar
}
}
At least, the debugger enters MyNavigationController but neither the searchbar is visible nor the red navigationbar.
I would suggest using a protocol with an extension that will provide the configuration of the navigation bar and item. Then you can extend any view controller to conform to it and use the protocol's default implementation.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureNavigationBar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: SearchController { }
protocol SearchController: class { }
extension SearchController where Self: UIViewController {
func configureNavigationBar() {
navigationController?.navigationBar.backgroundColor = UIColor.red
let search = UISearchBar()
search.placeholder = "Search"
search.showsCancelButton = true
navigationItem.titleView = search
}
}
I want to launch segue from tab bar item. When user touches an item on the tab bar. I want to launch a segue.
To do this I writed this code:
class TabBarController: UITabBarController, UITabBarControllerDelegate {
#IBOutlet var tabs: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if item.tag == 3 {
self.performSegueWithIdentifier("test123", sender: self)
}
}
}
Actually it works well except a problem. This is launching segue but also switching the tab. I don't want this. It should just launch start segue shouldn't switch the tab.
How can I prevent this problem?
Here's the minimal changes to get your code work
in your viewDidLoad() add
self.delegate = self
Then implement delegate method
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let shouldSelectIndex = tabBarController.viewControllers?.indexOf(viewController)
if shouldSelectIndex == 2
{
self.performSegueWithIdentifier("test123", sender: self)
return false
}
return true
}
That should work.
However I think you have design issues.
Subclass as a delegate is strange. Better separate delegate.
Instead of tag/indecies use introspection or another delegation or something
I Use the following code to check which tab is selected in TabBar in UIViewController not using UITabBarController, But i don't know how to load the Particular ViewController in the View, Or is there any other way to achieve this
This is what i get from the Google and other forums, I use the Tag for the UITabBarItems to differentiate the buttons
import UIKit
class AdminViewController: UIViewController, UITabBarDelegate {
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var tabbar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController();
menuButton.action = "revealToggle:";
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
}
tabbar.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
println(item.tag);
if(item.tag == 1)
{
//Want to load UIViewController into the CurrentViewController
}
}
}
I have another idea, But it is not good
Place the TabBar in all UIViewControllers and When the TabBarItem is Clicked i will Navigate to that UIViewController using PerformSegue & There also a TabBar
Yes #Oliver Borchert I used the tab bar controller
This solved my problem completely
TabBarController with SWRevealViewController
Thanks #Anbu.Karthik
I wish for the menu to be hidden when the front view controller is tapped on while the menu is visible.
I need to know an elegant solution to this that doesn't get me to add a gesturerecognizer on all my viewcontrollers
SWRevealViewController provides you with a tap gesture controller which is ready to use. So you can simply add it to your front controller :
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
Moreover, if you want to do it only once, you can create a controller which adds this gesture recognizer, and then inherit from this class. Example in Swift :
class YourFrontViewControllerParentClass : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let revealController = self.revealViewController() {
// add the tap gesture recognizer to the front view (RootViewController) so that the sidebar menu closes when the user taps the front view when the side menu is closed.
self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}
}
}
class YourFrontViewControllerChildClass1 : YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}
class YourFrontViewControllerChildClass2 : YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}
class YourFrontViewControllerChildClass3: YourFrontViewControllerParentClass {
override func viewDidLoad() {
super.viewDidLoad()
// specific stuff
}
}