Search code examples
iosuinavigationcontrolleruinavigationbarnavigation-draweruinavigationitem

Is it possible to place two buttons on left side of navigation bar with transformation of most left button on OnClick?


I want to display two buttons or can say two images on left side of navigation bar with same TouchUpInSide method. When I click on that two buttons the left most button should transform more to left and when i again click on the two buttons the left most button come back to original position. Or another way: I can place two UIImageView on left side of navigation bat and place one button with clear background and give event on that.

So is it possible and How?


Solution

  • If you want to add just simple UIButtons you can use the following method.

    1) Create multiple UIButton objects

    2) Add all those buttons to a UIView object

    3) Create UIBarButtonItem and pass the UIView as a custom view

    Code is as follows:

    // create the container view
    UIView* viewContainer = [[UIView alloc] init];
    
    // Create buttons and add it to the container view
    UIButton* btnAdd = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [btnAdd setTitle:@"Add" forState:UIControlStateNormal];
    [viewContainer addSubview:btnAdd];
    
    UIButton* btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [btnRefresh setTitle:@"Refresh" forState:UIControlStateNormal];
    [viewContainer addSubview:btnRefresh];
    
    // now create a Bar button item with custom view
    UIBarButtonItem* barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:viewContainer];
    
    // Set the navigation bar's right button item
    self.navigationItem.rightBarButtonItem = barBtnItem;
    

    or else if you want to add bar UIBarButtonItems go with adding multiple barbuttons as Anbu.Karthik said.

    Here is the code to add multiple bar buttons items.

    // Create UIToolbar to add two buttons in the right
    UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
    
    // Create standard "add" button
    UIBarButtonItem* btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
    btnAdd.style = UIBarButtonItemStyleBordered;
    
    // Create spacer
    UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    // create standard "refresh" button
    UIBarButtonItem* btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnRefresh_Clicked:)];
    btnRefresh.style = UIBarButtonItemStyleBordered;
    
    // stick the buttons in the UIToolbar
    [toolBar setItems:@[btnAdd, spacer, btnRefresh] animated:NO];
    
    // Put the toolbar in the UINavigationBar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
    

    Hope this will help you. :)