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]
The items to display on the tab bar.
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.