Showing posts with label UITabBar. Show all posts
Showing posts with label UITabBar. Show all posts

Monday, June 10, 2013

UITabBar setItems animated example in Objective C (iOS).


UITabBar setItems animated

Sets the items on the tab bar, with or without animation.

- (void)setItems:(NSArray *)items animated:(BOOL)animated

Parameters of [UITabBar setItems animated]
items
The items to display on the tab bar.
animated
If YES, animates the transition to the items; otherwise, does not.

Discussion of [UITabBar setItems animated]
If animated is YES, the changes are dissolved or the reordering is animated—for example, removed items fade out and new items fade in. This method also adjusts the spacing between items.

UITabBar setItems animated example.
You should do something like below:

    UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
    UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];

    NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];

    [tabbar setItems:arrTabbarItems animated:NO];

Example of [UITabBar setItems animated].
UITabBarItem *firstItem = [UITabBarItem initWithTitle:@"First" image:firstImage tag:1];
UITabBarItem *secondItem = [UITabBarItem initWithTitle:@"Second" image:secondImage tag:2];

NSArray *itemsArray = [NSArray arrayWithItems:firstItem, secondItem, nil];

[myTabBar setItems:itemsArray animated:YES];

UITabBar setItems animated example.
UITabBar has an NSArray collection of items. Since the items property is an NSArray and not an NSMutableArray, you'd have to construct a new NSArray from the existing one devoid of the object you want to remove, then set the items property to the new array.

/* suppose we have a UITabBar *myBar, and an int index idx */
NSMutableArray modifyMe = [[myBar items] mutableCopy];
[modifyMe removeObjectAtIndex:idx];
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];
[myBar setItems:newItems animated:true];

End of UITabBar setItems animated example article.

UITabBar setItems example in Objective C (iOS).


UITabBar setItems

Sets the items on the tab bar, with or without animation.

- (void)setItems:(NSArray *)items animated:(BOOL)animated

Parameters of [UITabBar setItems]
items
The items to display on the tab bar.
animated
If YES, animates the transition to the items; otherwise, does not.

Discussion of [UITabBar setItems]
If animated is YES, the changes are dissolved or the reordering is animated—for example, removed items fade out and new items fade in. This method also adjusts the spacing between items.

UITabBar setItems example.
You should do something like below:

    UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
    UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];

    NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];

    [tabbar setItems:arrTabbarItems animated:NO];

Example of [UITabBar setItems].
UITabBarItem *firstItem = [UITabBarItem initWithTitle:@"First" image:firstImage tag:1];
UITabBarItem *secondItem = [UITabBarItem initWithTitle:@"Second" image:secondImage tag:2];

NSArray *itemsArray = [NSArray arrayWithItems:firstItem, secondItem, nil];

[myTabBar setItems:itemsArray animated:YES];

UITabBar setItems example.
UITabBar has an NSArray collection of items. Since the items property is an NSArray and not an NSMutableArray, you'd have to construct a new NSArray from the existing one devoid of the object you want to remove, then set the items property to the new array.

/* suppose we have a UITabBar *myBar, and an int index idx */
NSMutableArray modifyMe = [[myBar items] mutableCopy];
[modifyMe removeObjectAtIndex:idx];
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];
[myBar setItems:newItems animated:true];

End of UITabBar setItems example article.

UITabBar beginCustomizingItems example in Objective C (iOS).


UITabBar beginCustomizingItems

Presents a modal view allowing the user to customize the tab bar by adding, removing, and rearranging items on the tab bar.

- (void)beginCustomizingItems:(NSArray *)items

Parameters of [UITabBar beginCustomizingItems]
items
The items to display on the modal view that can be rearranged.
The items parameter should contain all items that can be added to the tab bar. Visible items not in items are fixed in place—they can not be removed or replaced by the user.

Discussion of [UITabBar beginCustomizingItems]
Use this method to start customizing a tab bar. For example, create an Edit button that invokes this method when tapped. A modal view appears displaying all the items in items with a Done button at the top. Tapping the Done button dismisses the modal view. If the selected item is removed from the tab bar, the selectedItem property is set to nil. Set the delegate property to an object conforming to the UITabBarDelegate protocol to further modify this behavior.

UITabBar beginCustomizingItems example.
Well, then it's a good thing that UITabBar has this functionality built in. Take a look at the following method:

UITabBar *mybar = [[UITabBar alloc] init];
[mybar beginCustomizingItems:[NSArray arrayWithObjects:tabItem1, tabItem2, nil]];

End of UITabBar beginCustomizingItems example article.

UITabBar setTintColor example in Objective C (iOS).


UITabBar setTintColor

The tint color to apply to the tab bar background.

@property(nonatomic, retain) UIColor *tintColor

UITabBar setTintColor example.
for me its very simple to change the color of Tabbar like :-

[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];

[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];

Example of [UITabBar setTintColor].
self.tabBarController.tabBar.tintColor = [[UIColor alloc] initWithRed:0.00
                                                                green:0.62
                                                                 blue:0.93
                                                                alpha:1.0];

UITabBar setTintColor example.
- (void)createTabBar:(NSArray*)arguments withDict:
(NSDictionary*)options
{
    UIColor *myNewColor = UIColorFromRGB(0xCD2626);
    tabBar = [UITabBar new];
    [tabBar sizeToFit];
    tabBar.delegate = self;
    tabBar.multipleTouchEnabled   = NO;
    tabBar.autoresizesSubviews    = YES;
    tabBar.hidden                 = YES;
    tabBar.userInteractionEnabled = YES;
        tabBar.opaque = YES;
    tabBar.tintColor = myNewColor;

        self.webView.superview.autoresizesSubviews = YES;

        [ self.webView.superview addSubview:tabBar];
} 

End of UITabBar setTintColor example article.

UITabBar tintColor example in Objective C (iOS).


UITabBar tintColor

The tint color to apply to the tab bar background.

@property(nonatomic, retain) UIColor *tintColor

UITabBar tintColor example.
for me its very simple to change the color of Tabbar like :-

[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];

[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];

Example of [UITabBar tintColor].
self.tabBarController.tabBar.tintColor = [[UIColor alloc] initWithRed:0.00
                                                                green:0.62
                                                                 blue:0.93
                                                                alpha:1.0];

UITabBar tintColor example.
- (void)createTabBar:(NSArray*)arguments withDict:
(NSDictionary*)options
{
    UIColor *myNewColor = UIColorFromRGB(0xCD2626);
    tabBar = [UITabBar new];
    [tabBar sizeToFit];
    tabBar.delegate = self;
    tabBar.multipleTouchEnabled   = NO;
    tabBar.autoresizesSubviews    = YES;
    tabBar.hidden                 = YES;
    tabBar.userInteractionEnabled = YES;
        tabBar.opaque = YES;
    tabBar.tintColor = myNewColor;

        self.webView.superview.autoresizesSubviews = YES;

        [ self.webView.superview addSubview:tabBar];
} 

End of UITabBar tintColor example article.

UITabBar setSelectionIndicatorImage example in Objective C (iOS).


UITabBar setSelectionIndicatorImage

The image used for the selection indicator.

@property(nonatomic, retain) UIImage *selectionIndicatorImage

Discussion of [UITabBar setSelectionIndicatorImage]
The selection indicator image is drawn on top of the tab bar, behind the bar item icon.

UITabBar setSelectionIndicatorImage example.
I first include <UITabBarControllerDelegate> in the .h of my app delegate. And in the didFinishLaunchingWithOptions method I set the delegate of the tab bar:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
Then I can use this method to toggle whether or not to show the background image or not:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    UITabBar *tabBar = tabBarController.tabBar;
    if (tabBar.selectedItem == [tabBar.items objectAtIndex:2]) {
        [tabBar setSelectionIndicatorImage:[[UIImage alloc] init]];
    }
    else {
        [tabBar setSelectionIndicatorImage:nil];
    }
}

Example of [UITabBar setSelectionIndicatorImage].
Try this code:

// iOS 5.0+
[self.tabBar setSelectionIndicatorImage:[[[UIImage alloc] init]autorelease]];

// for earlier versions

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self customizeTabBar];
}

- (void)customizeTabBar {
    NSString *imageName = [NSString stringWithFormat:@"tabBackground%i.png", tabBarCtrl.selectedIndex + 1];

    for(UIView *view in tabBarCtrl.tabBar.subviews) { 
         if([view isKindOfClass:[UIImageView class]]) { 
              [view removeFromSuperview]; 
         } 
    } 
    UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    [tabBarCtrl.tabBar insertSubview:background atIndex:0];
    [tabBarCtrl.tabBar bringSubviewToFront:background];
    //if needed, here must be adding UILabels with titles, I didn't need it.
} 

UITabBar setSelectionIndicatorImage example.
//create new tab bar appearance
UITabBar *tabBar = [UITabBar appearance];
//set background image
[tabBar setBackgroundImage:[UIImage imageNamed:@"menu.png"]];
//create a colour and apply it as tint colour when items aren't selected.
UIColor *color=[UIColor colorWithRed:64.0/255.0 green:147.0/255.0 blue:52.0/255.0 alpha:255.0];
UIColor *colorSelected=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
[tabBar setTintColor:color];
[tabBar setSelectedImageTintColor:colorSelected];

[tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selection-tab.png"]];

End of UITabBar setSelectionIndicatorImage example article.

UITabBar selectionIndicatorImage example in Objective C (iOS).


UITabBar selectionIndicatorImage

The image used for the selection indicator.

@property(nonatomic, retain) UIImage *selectionIndicatorImage

Discussion of [UITabBar selectionIndicatorImage]
The selection indicator image is drawn on top of the tab bar, behind the bar item icon.

UITabBar selectionIndicatorImage example.
I first include <UITabBarControllerDelegate> in the .h of my app delegate. And in the didFinishLaunchingWithOptions method I set the delegate of the tab bar:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
Then I can use this method to toggle whether or not to show the background image or not:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    UITabBar *tabBar = tabBarController.tabBar;
    if (tabBar.selectedItem == [tabBar.items objectAtIndex:2]) {
        [tabBar setSelectionIndicatorImage:[[UIImage alloc] init]];
    }
    else {
        [tabBar setSelectionIndicatorImage:nil];
    }
}

Example of [UITabBar selectionIndicatorImage].
Try this code:

// iOS 5.0+
[self.tabBar setSelectionIndicatorImage:[[[UIImage alloc] init]autorelease]];

// for earlier versions

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self customizeTabBar];
}

- (void)customizeTabBar {
    NSString *imageName = [NSString stringWithFormat:@"tabBackground%i.png", tabBarCtrl.selectedIndex + 1];

    for(UIView *view in tabBarCtrl.tabBar.subviews) { 
         if([view isKindOfClass:[UIImageView class]]) { 
              [view removeFromSuperview]; 
         } 
    } 
    UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    [tabBarCtrl.tabBar insertSubview:background atIndex:0];
    [tabBarCtrl.tabBar bringSubviewToFront:background];
    //if needed, here must be adding UILabels with titles, I didn't need it.
} 

UITabBar selectionIndicatorImage example.
//create new tab bar appearance
UITabBar *tabBar = [UITabBar appearance];
//set background image
[tabBar setBackgroundImage:[UIImage imageNamed:@"menu.png"]];
//create a colour and apply it as tint colour when items aren't selected.
UIColor *color=[UIColor colorWithRed:64.0/255.0 green:147.0/255.0 blue:52.0/255.0 alpha:255.0];
UIColor *colorSelected=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
[tabBar setTintColor:color];
[tabBar setSelectedImageTintColor:colorSelected];

[tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"selection-tab.png"]];

End of UITabBar selectionIndicatorImage example article.

UITabBar setSelectedItem example in Objective C (iOS).


UITabBar setSelectedItem

The currently selected item on the tab bar.

@property(nonatomic, assign) UITabBarItem *selectedItem

Discussion of [UITabBar setSelectedItem]
Changes to this property show visual feedback in the user interface. The selected and unselected images displayed by an item are automatically created based on the alpha values in its original image property that you set. The default value is nil.

UITabBar setSelectedItem example.
UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:@"About" image:[UIImage imageNamed:@"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];

Example of [UITabBar setSelectedItem].
Can't you just call your method to select a tab whenever you display the view? Like so:

[self activateTab:1];
To change which tab bar item is selected use:

[myTabBar setSelectedItem:myTabBarItem];
Where myTabBarItem is your UITabBarItem instance for the relevant view.


UITabBar setSelectedItem example.
- (void)viewDidLoad
{
 UITabBarItem *item = [myTabBar.items objectAtIndex:0];
 [self.myTabBar setSelectedItem:item];
 if(tab1Exam == nil){
  self.tab1Exam = [[CurExam alloc] initWithNibName:@"CurExam" bundle:nil];
 [self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
 if (currentViewController != nil)
  [currentViewController.view removeFromSuperview];
 currentViewController = tab1Exam;
 }
}

End of UITabBar setSelectedItem example article.

UITabBar selectedItem example in Objective C (iOS).


UITabBar selectedItem

The currently selected item on the tab bar.

@property(nonatomic, assign) UITabBarItem *selectedItem

Discussion of [UITabBar selectedItem]
Changes to this property show visual feedback in the user interface. The selected and unselected images displayed by an item are automatically created based on the alpha values in its original image property that you set. The default value is nil.

UITabBar selectedItem example.
UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:@"About" image:[UIImage imageNamed:@"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];

Example of [UITabBar selectedItem].
Can't you just call your method to select a tab whenever you display the view? Like so:

[self activateTab:1];
To change which tab bar item is selected use:

[myTabBar setSelectedItem:myTabBarItem];
Where myTabBarItem is your UITabBarItem instance for the relevant view.


UITabBar selectedItem example.
- (void)viewDidLoad
{
 UITabBarItem *item = [myTabBar.items objectAtIndex:0];
 [self.myTabBar setSelectedItem:item];
 if(tab1Exam == nil){
  self.tab1Exam = [[CurExam alloc] initWithNibName:@"CurExam" bundle:nil];
 [self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
 if (currentViewController != nil)
  [currentViewController.view removeFromSuperview];
 currentViewController = tab1Exam;
 }
}

End of UITabBar selectedItem example article.

UITabBar setSelectedImageTintColor example in Objective C (iOS).


UITabBar setSelectedImageTintColor

The tint color to apply to the gradient image used when creating the selected image.

@property(nonatomic, retain) UIColor *selectedImageTintColor

Discussion of [UITabBar setSelectedImageTintColor]
The default value is nil, which results in the system bright blue being used for selected tab item images.

If you want to also customize the unselected image appearance, you must sent setFinishedSelectedImage:withFinishedUnselectedImage: to individual tab bar items.

UITabBar setSelectedImageTintColor example.
// custom icons
UITabBarItem *item = [[UITabBarItem alloc] init];
item.title = @"foo";
// setting custom images prevents the OS from applying a tint color
[item setFinishedSelectedImage:[UIImage imageNamed:@"tab1_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab1_image_deselected.png"]];
tab1ViewController.tabBarItem = item;

    // tab bar

    // set background image - will be used instead of glossy black
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar_bg.png"];
    // optionally set the tint color - setting this ti nil will result in the standard, blue tint color. tint color is ignored when custom icons are set as above.
    tabBarController.tabBar.selectedImageTintColor = nil;
    // remove the highlight around the selected tab - or provide an alternate highlight image. If you don't do this the iOS default is to draw a highlighted box beneath the selected tab icon.
    tabBarController.tabBar.selectionIndicatorImage = [[UIImage alloc] init];

Example of [UITabBar setSelectedImageTintColor].
For iOS5 and upwards, you can just do this:

[rootTabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]];

UITabBar setSelectedImageTintColor example.
In AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
return YES;
}

End of UITabBar setSelectedImageTintColor example article.

UITabBar selectedImageTintColor example in Objective C (iOS).


UITabBar selectedImageTintColor

The tint color to apply to the gradient image used when creating the selected image.

@property(nonatomic, retain) UIColor *selectedImageTintColor

Discussion of [UITabBar selectedImageTintColor]
The default value is nil, which results in the system bright blue being used for selected tab item images.

If you want to also customize the unselected image appearance, you must sent setFinishedSelectedImage:withFinishedUnselectedImage: to individual tab bar items.

UITabBar selectedImageTintColor example.
// custom icons
UITabBarItem *item = [[UITabBarItem alloc] init];
item.title = @"foo";
// setting custom images prevents the OS from applying a tint color
[item setFinishedSelectedImage:[UIImage imageNamed:@"tab1_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab1_image_deselected.png"]];
tab1ViewController.tabBarItem = item;

    // tab bar

    // set background image - will be used instead of glossy black
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar_bg.png"];
    // optionally set the tint color - setting this ti nil will result in the standard, blue tint color. tint color is ignored when custom icons are set as above.
    tabBarController.tabBar.selectedImageTintColor = nil;
    // remove the highlight around the selected tab - or provide an alternate highlight image. If you don't do this the iOS default is to draw a highlighted box beneath the selected tab icon.
    tabBarController.tabBar.selectionIndicatorImage = [[UIImage alloc] init];

Example of [UITabBar selectedImageTintColor].
For iOS5 and upwards, you can just do this:

rootTabBarController.tabBar.selectedImageTintColor = [UIColor greenColor];

UITabBar selectedImageTintColor example.
In AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
return YES;
}

End of UITabBar selectedImageTintColor example article.

UITabBar setBackgroundImage example in Objective C (iOS).


UITabBar setBackgroundImage

The background image for the bar.

@property(nonatomic, retain) UIImage *backgroundImage

Discussion of [UITabBar setBackgroundImage]
A stretchable background image is stretched, a non-stretchable background image is tiled.

UITabBar setBackgroundImage example.
Like mentioned before on iOS 5 I would suggest you use the background image:

UITabBar *tabBar = tabController.tabBar;
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    tabBar.backgroundImage = [UIImage imageNamed:@"TabBackground.png"];
}

Example of [UITabBar setBackgroundImage].
// UITabBar+Custom.m

#import "UITabBar+Custom.h"
#import <QuartzCore/QuartzCore.h>

-(void)setTabBarBackground:(UIImage *)backgroundImage  {
     if([self respondsToSelector:@selector(setBackgroundImage:)]) {
          // ios 5+
          [self setBackgroundImage:backgroundImage];
     }  else  {
          // ios 3.x / 4.x
          self.layer.contents = (id)backgroundImage.CGImage;
     }
}

UITabBar setBackgroundImage example.
// not supported on iOS4   
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    // set it just for this instance
    [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]];

    // set for all
    // [[UITabBar appearance] setBackgroundImage: ...
}
else
{
    // ios 4 code here
}

End of UITabBar setBackgroundImage example article.

UITabBar backgroundImage example in Objective C (iOS).


UITabBar backgroundImage

The background image for the bar.

@property(nonatomic, retain) UIImage *backgroundImage

Discussion of [UITabBar backgroundImage]
A stretchable background image is stretched, a non-stretchable background image is tiled.

UITabBar backgroundImage example.
Like mentioned before on iOS 5 I would suggest you use the background image:

UITabBar *tabBar = tabController.tabBar;
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    tabBar.backgroundImage = [UIImage imageNamed:@"TabBackground.png"];
}

Example of [UITabBar backgroundImage].
// UITabBar+Custom.m

#import "UITabBar+Custom.h"
#import <QuartzCore/QuartzCore.h>

-(void)setTabBarBackground:(UIImage *)backgroundImage  {
     if([self respondsToSelector:@selector(setBackgroundImage:)]) {
          // ios 5+
          [self setBackgroundImage:backgroundImage];
     }  else  {
          // ios 3.x / 4.x
          self.layer.contents = (id)backgroundImage.CGImage;
     }
}

UITabBar backgroundImage example.
// not supported on iOS4   
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    // set it just for this instance
    [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]];

    // set for all
    // [[UITabBar appearance] setBackgroundImage: ...
}
else
{
    // ios 4 code here
}

End of UITabBar backgroundImage example article.