Showing posts with label setItems example. Show all posts
Showing posts with label setItems example. Show all posts

Tuesday, June 11, 2013

UIToolbar setItems example in Objective C (iOS).

UIToolbar setItems

Sets the items on the toolbar by animating the changes.

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

Parameters of [UIToolbar setItems]
items
The items to display on the toolbar.
animated
A Boolean value if set to YES animates the transition to the items; otherwise, does not.

Discussion of [UIToolbar 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.

UIToolbar setItems example.
NSMutableArray *items = [[self.toolbar items] mutableCopy];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer];
[spacer release];

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setTextAlignment:UITextAlignmentCenter];

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
[spacer2 release];

UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];
[items addObject:title];
[title release];

[self.toolbar setItems:items animated:YES];
[items release];

Example of [UIToolbar setItems].
- (void)viewDidLoad {
[super viewDidLoad];// Add an invisible UIActivityViewIndicator to the toolbar
UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:767];
NSArray *items = [toolbar items];

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];

NSArray *newItems = [NSArray arrayWithObjects:[items objectAtIndex:0],[items objectAtIndex:1],[items objectAtIndex:2],
 [[UIBarButtonItem alloc] initWithCustomView:activityIndicator], [items objectAtIndex:3],nil];
[toolbar setItems:newItems];}

UIToolbar setItems example.
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithObjects...]];
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];

End of UIToolbar setItems example article.

Monday, June 10, 2013

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.

Sunday, June 9, 2013

UINavigationBar setItems example in Objective C (iOS).


UINavigationBar setItems

Replaces the navigation items currently managed by the navigation bar with the specified items.

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

Parameters of [UINavigationBar setItems]
items
The UINavigationItem objects to place in the stack. The front-to-back order of the items in this array represents the new bottom-to-top order of the items in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack.
animated
If YES, animate the pushing or popping of the top stack item. If NO, replace the stack items without any animations.

Discussion of [UINavigationBar setItems]
You can use this method to update or replace the navigation items in the stack without pushing or popping each item explicitly. In addition, this method lets you update the stack without animating the changes, which might be appropriate at launch time when you want to restore the state of the navigation stack to some previous state.[UINavigationBar setItems]

If animations are enabled, this method decides which type of transition to perform based on whether the last item in the items array is already on the current navigation stack. If the item is currently on the stack, but is not the topmost item, this method uses a pop transition; if it is the topmost item, no transition is performed. If the item is not on the stack, this method uses a push transition. Only one transition is performed, but when that transition finishes, the entire contents of the stack are replaced with the new items. For example, if items A, B, and C are on the stack and you set items D, A, and B, this method uses a pop transition and the resulting stack contains the items D, A, and B.

UINavigationBar setItems example.
// This code is used for a custom navigation bar

UINavigationItem* newItem = [[UINavigationItem alloc] initWithTitle:@""];
[newItem setTitleView:segmentedControl];

// Assuming you already have a navigation bar called "navigationBar"
[navigationBar setItems:[NSArray arrayWithObject:newItem] animated:NO];

// No memory leaks please...
[newItem release];

Example of [UINavigationBar setItems].
[super viewDidLoad];
    _navBar = [[UINavigationBar alloc] init];
    [_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
    [self.view addSubview:_navBar];
    UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(buttonSettingClicked)];
    [navItem setLeftBarButtonItem:barButton];

    [_navBar setItems:@[navItem]];

UINavigationBar setItems example.
- (void)viewDidLoad
{
    [super viewDidLoad];
    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [navigationBar setItems:[NSArray arrayWithObject:[[UINavigationItem alloc]initWithTitle:@"Title"]]];
    navigationBar.topItem.titleView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:navigationBar];
}

End of UINavigationBar setItems example article.