Sunday, June 9, 2013

UINavigationItem setRightBarButtonItems animated example in Objective C (iOS).


UINavigationItem setRightBarButtonItems animated

Sets the right bar button items, optionally animating the transition to the new items.

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

Parameters of [UINavigationItem setRightBarButtonItems animated]
items
An array of custom bar button items to display on the right side of the navigation bar.
animated
Specify YES to animate the transition to the custom bar items when this item is the top item. Specify NO to set the items immediately without animating the change.

Discussion of [UINavigationItem setRightBarButtonItems animated]
If two navigation items have the same custom left or right bar button items, those bar button items remain stationary during the transition when the navigation item is pushed or popped.

UINavigationItem setRightBarButtonItems animated example.
With iOS 5+ it's as easy as:

UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(loadBeeconSite)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil] animated:NO];

Example of [UINavigationItem setRightBarButtonItems animated].
With iOS 5+
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
[previewer setDataSource:self];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"Salva Documento" style:UIBarButtonItemStyleBordered target:self action:@selector(saveFileToDocuments)];
NSArray *buttons = [NSArray arrayWithObjects:[[previewer navigationItem]rightBarButtonItem],saveButton, nil];
[[previewer navigationItem] setRightBarButtonItems:buttons animated:NO];

UINavigationItem setRightBarButtonItems animated example.
UINavigationBar *navBarView = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 42.0f)];
    navBarView.tintColor= [UIColor colorWithRed:90.0/255.0f green:53.0/255.0f blue:45.0/255.0f alpha:1.0];

    UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backView)];
    UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(SaveImage)];
    UIBarButtonItem *Add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddComment:)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
    navigationItem.leftBarButtonItem = left;
    NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
    [buttonArray addObject:right];
    [buttonArray addObject:Add];
    [navigationItem setRightBarButtonItems:buttonArray animated:NO];
    [navBarView pushNavigationItem:navigationItem animated:NO];
[self.view addSubView:navBarView];

End of UINavigationItem setRightBarButtonItems animated example article.