How to add a UIToolbar programmatically to an iOS app?

后端 未结 7 1082
太阳男子
太阳男子 2020-12-04 10:01

Can\'t seem to find a tutorial which does as the question title describes. I\'d like to understand just where the UIToolbar needs to be declared and how to get it onto my vi

相关标签:
7条回答
  • 2020-12-04 10:57

    If you're using UINavigationController then the toolbar comes with it by default.

    You can add it using following line of code:

    self.navigationController.toolbarHidden = NO;
    

    And to add button to your Toolbar you can use following code:

    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    

    flexibleItem is used to maintain proper distance between the two buttons that we have created above.

    Now you can add these three items in order to make them visible on your view.

    NSArray *items = [NSArray arrayWithObjects:item1, flexibleItem, item2, nil];   
    self.toolbarItems = items;
    

    I hope it works for you.

    0 讨论(0)
提交回复
热议问题