ios 如何以编程方式将 UIToolbar 添加到 UITableViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5958956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:51:51  来源:igfitidea点击:

How to add a UIToolbar to a UITableViewController programmatically?

iphoneiosuitableviewuitoolbar

提问by jini

I have opted to use a UITableViewControllerwithout a nib. I need a UIToolbar at the bottom with two buttons. What is the simplest way of doing that?

我选择使用UITableViewController没有笔尖的。我需要一个底部有两个按钮的 UIToolbar。这样做的最简单方法是什么?

P.S. I know that I can easily use a UIViewControllerand add a UITableViewhowever I want things to look consistent across the app.

PS 我知道我可以轻松地使用 aUIViewController并添加 aUITableView但是我希望整个应用程序看起来一致。

Can someone help?

有人可以帮忙吗?

I saw the following example and I am not sure on its validity:

我看到了以下示例,但不确定其有效性:

(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //Initialize the toolbar 
    toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit];

    //Caclulate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea];

    //Create a button 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back"
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(info_clicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];

    [[self tableView] reloadData];
}

(void) info_clicked:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
    [toolbar removeFromSuperview];

}

回答by more tension

The simpler thing to do is to build your project on top of a UINavigationController. It already has a toolbar, it's just hidden by default. You can reveal it by toggling the toolbarHiddenproperty, and your table view controller will be able to use it as long as it's in the navigation controller hierarchy.

更简单的做法是在UINavigationController. 它已经有一个工具栏,默认情况下它只是隐藏的。你可以通过切换toolbarHidden属性来显示它,只要它在导航控制器层次结构中,你的表视图控制器就可以使用它。

In your app delegate, or in the object your app delegate passes control to, create the navigation controller with your UITableViewControlleras the root view controller:

在您的应用程序委托中,或在您的应用程序委托将控制权传递给的对象中,以您UITableViewController的根视图控制器创建导航控制器:

- ( void )application: (UIApplication *)application
          didFinishLaunchingWithOptions: (NSDictionary *)options
{
    MyTableViewController         *tableViewController;
    UINavigationController        *navController;

    tableViewController = [[ MyTableViewController alloc ]
                                 initWithStyle: UITableViewStylePlain ];
    navController = [[ UINavigationController alloc ]
                           initWithRootViewController: tableViewController ];
    [ tableViewController release ];

    /* ensure that the toolbar is visible */
    navController.toolbarHidden = NO;
    self.navigationController = navController;
    [ navController release ];

    [ self.window addSubview: self.navigationController.view ];
    [ self.window makeKeyAndVisible ];
}

Then set the toolbar items in your MyTableViewControllerobject:

然后在您的MyTableViewController对象中设置工具栏项目:

- ( void )viewDidLoad
{
    UIBarButtonItem            *buttonItem;

    buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back"
                                            style: UIBarButtonItemStyleBordered
                                            target: self
                                            action: @selector( goBack: ) ];
    self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
    [ buttonItem release ];

    /* ... additional setup ... */
}

回答by Vladimir Shutyuk

You also can just check "shows toolbar" option in NavigationController attributes inspector.

您也可以在 NavigationController 属性检查器中选中“显示工具栏”选项。

回答by prodeveloper

Here is a simple example, which may help

这是一个简单的例子,可能会有所帮助

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteMessages)];
    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeMail)];
    NSArray *toolbarItems = [NSMutableArray arrayWithObjects:spaceItem, trashItem,spaceItem,composeItem,nil];
    self.navigationController.toolbarHidden = NO;
    [self setToolbarItems:toolbarItems];

Thanks, prodeveloper

谢谢,开发者