Sunday, June 9, 2013

UINavigationItem leftBarButtonItem example in Objective C (iOS).


UINavigationItem leftBarButtonItem

A custom bar button item displayed on the left of the navigation bar when the receiver is the top navigation item.

@property(nonatomic, retain) UIBarButtonItem *leftBarButtonItem

Discussion of [UINavigationItem leftBarButtonItem]
In iOS 5.0 and later, the contents of this property always refer to the first bar button item in the leftBarButtonItems array. Assigning a new value to this property replaces the first item in the leftBarButtonItems array with the new value. Setting this property to nil removes the first item in the array. If the bar button item is already in the array, it is moved from its current location to the front of the array.

Prior to iOS 5.0, this property contained the single bar item to display on the left side of the navigation bar instead of the back button.

UINavigationItem leftBarButtonItem example.
Hiding UInavigationItem's bar button
This works I tried it myself

self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;      

Example of [UINavigationItem leftBarButtonItem].
CGRect rect = CGRectMake(10, 0, 30, 30);
self.backButton = [[UIButton alloc] initWithFrame:rect];
[self.backButton setImage:[UIImage imageNamed:@"back_arrow.png"]
                 forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
                    action:@selector(backButtonAction:)
          forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
self.navigationItem.hidesBackButton = YES;

UINavigationItem leftBarButtonItem example.
   //Next Button
    btnNext = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
               UIBarButtonSystemItemPlay  target:self action:@selector(next:)];
    [items addObject:btnNext];

    [tool setItems:items];
    tool.barStyle =UIBarStyleDefault;
    tool.backgroundColor = [UIColor clearColor];

    //self.navigationItem.leftBarButtonItem.customView = tool;
    //self.navigationItem.titleView = tool;
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:tool];

End of UINavigationItem leftBarButtonItem example article.