Cannot display toolbar items in NavigationController using Swift

I am trying to display the toolbar items at the bottom of the TableViewController, which is inside the navigation controller. Ive written this code in Swift.

I used the Xcode homepage template template to create a project and wrote the code below in the ViewDidLoad method for MasterTableViewController.

Help me solve the problem.

The following is a snippet of code.

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 it’s interesting that the same code written in Objective-C works and shows a toolbar at the bottom with two elements

- (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];
}
+4
source share
2 answers

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

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

Basically this code (with animation):

self.toolbarItems = self.toolbarItems

toolbarItems . .

self.setToolbarItems(toolBarItems, animated: true), .

+2

NSMutableArray ,

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
}
+1

All Articles