IPhone: How can I create my own TabBar?

Since I have some tab requirements that a regular iphone panel cannot provide, I need to create my own.

What is the best way to create my own tab, in particular, how to correctly add / remove (show / hide) views in my main view controller, taking into account the memory and best practices for subtitles?

+4
source share
3 answers

As I said elsewhere, it is almost never a good idea to get rid of the basic navigation classes provided by UIKit . What are your requirements for applications that you think deserve a fully customizable tab bar class? You can almost always achieve the necessary settings by subclassing, categorizing, or using layers.

UPDATE 1 . So, here is what I did in some of my applications to create a custom tab bar.

  • Subclass UITabBar
  • Add a method to your subclass called -updateTabBarImageForViewControllerIndex:
  • In the Builder interface, change the tab bar class of the tab bar to your own subclass
  • In any class that matches the tab bar delegate delegate (for example, the application delegate), implement -tabBarController:shouldSelectViewController: and call -updateTabBarImageForViewControllerIndex: in your sub-tab tab bar

Basically, you want to notify your tab bar subclass every time a tab bar controller is about to switch view controllers. When this happens, determine which image to select for the tab bar. You must have n images for your tab bar, one for the selected state of each tab. This allows you to actually clean up the UITabBarItem implementation and just work with individual images, but it works a bit more.

 // MyAppDelegate.m - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { // Determine the index based on the selected view controller NSUInteger viewControllerIndex = ...; [(MyTabBar *)tabBarController.tabBar updateTabBarImageForViewControllerIndex:viewControllerIndex]; return YES; } // MyTabBar.m - (void)updateTabBarImageForViewControllerIndex:(NSUInteger)index { // Determine the image name based on the selected view controller index self.selectedTabBarImage = [UIImage imageNamed:...]; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, self.selectedTabBarImage.CGImage); } 

UPDATE 2 . Now that I think about it more, you could (and should) get away from what you are trying to achieve without a subclass of UITabBar at all. Import <QuartzCore/QuartzCore.h> and use the contents of the layer. :)

 // MyAppDelegate.m - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { // Determine the image name based on the selected view controller CGImageRef newTabBarImageRef = [[UIImage imageNamed:...] CGImage]; tabBarController.tabBar.layer.contents = (id)newTabBarImageRef; return YES; } 
+6
source

It really depends on your application. If you can afford to store in memory all the view controllers assigned to your tab, then you can use a simple array that will store all the corresponding view controllers, and you will show them using the index in that array. It's also a good idea to create your own custom view controller, which will save its own image as a tab (and / or name). And your contributor will take all these values ​​from there.

If you cannot afford so much memory (but this is not very possible), you can store the NSDictionary in an array and not look at the controllers. And when the user clicks on the tab element, you simply unload the previous view controller and create a new one, with parameters from this dictionary. Or instead of a dictionary, you can use some kind of custom container class.

0
source

Two accepted answers are interrupted in iOS5. Instead, I used to save the user subclass of the tab bar and left only this method intact:

 - (void)drawRect:(CGRect)rect { CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, nil); } 

Then in MainWindow.xib I created a custom view with the image as the background and UIButtons as the elements of the tab bar.

In the TabController delegate, I update the selected button states.

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { // Determine the index based on the selected view controller UIButton *newBtn; if(viewController == homeVC) { ... } // Update the buttons newBtn.selected = YES; _selectedTabBtn.selected = NO; _selectedTabBtn = newBtn; return YES; } 
0
source

Source: https://habr.com/ru/post/1338608/


All Articles