user2988824
user2988824

Reputation: 41

Unable to display Toolbar items in a NavigationController using Swift

I am trying to display toolbar items at the bottom of TableViewController which is inside a Navigation Controller. I’ve written this code in Swift.

I've used Xcode default master-detail template to create the project and written the below code in ViewDidLoad method of MasterTableViewController.

Please help me to fix the issue.

Please find below code snippet.

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.addToolBar();
}


func addToolBar ()->Void {

        self.hidesBottomBarWhenPushed = false

        var toolBarItems = NSMutableArray()

        var systemButton1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: nil, action: nil)
        toolBarItems.addObject(systemButton1)

        var systemButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        toolBarItems.addObject(systemButton2)

        var systemButton3 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Trash, target: nil, action: nil)
        toolBarItems.addObject(systemButton3)

        self.navigationController?.toolbarHidden = false
        self.setToolbarItems(toolbarItems, animated: true)
        //self.navigationController?.toolbarItems = toolbarItems;

    }

But Interestingly, the same code written in Objective-C works and shows the toolbar at the bottom with two items

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    [self addToolbar];
}

-(void) addToolbar
{
    self.hidesBottomBarWhenPushed = NO;

    NSMutableArray *items = [[NSMutableArray alloc] init];

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil];
    [items addObject:item1];

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

    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
    [items addObject:item3];


    self.navigationController.toolbarHidden = NO;
//    self.navigationController.toolbarItems = items;
//    
    [self setToolbarItems:items animated:YES];
}

Upvotes: 4

Views: 7369

Answers (2)

anoop4real
anoop4real

Reputation: 7718

Using NSMutableArray gave me error, the below code worked for me

func setUpToolbar(){

    var toolBarItems = [UIBarButtonItem]()

    let systemButton1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: nil, action: nil)
    toolBarItems.append(systemButton1)

    let systemButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    toolBarItems.append(systemButton2)

    let systemButton3 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Trash, target: nil, action: nil)
    toolBarItems.append(systemButton3)

    self.setToolbarItems(toolBarItems, animated: true)
    self.navigationController?.toolbarHidden = false
}

Upvotes: 2

Matthias Bauch
Matthias Bauch

Reputation: 90127

You have a tiny typo in your code. I highlighted the difference for you:

var toolBarItems = NSMutableArray()
//      ^
// [...]
self.setToolbarItems(toolbarItems, animated: true)
//                       ^

Your code does basically this (with an animation):

self.toolbarItems = self.toolbarItems

Your set the toolbarItems array to the current toolbarItems array, which is empty.

when you use self.setToolbarItems(toolBarItems, animated: true) it will work.

Upvotes: 2

Related Questions