【问题标题】:How to add a UIToolbar to a UITableViewController programmatically?如何以编程方式将 UIToolbar 添加到 UITableViewController?
【发布时间】:2011-08-22 23:18:03
【问题描述】:

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

附:我知道我可以轻松使用UIViewController 并添加UITableView,但是我希望整个应用程序看起来一致。

有人可以帮忙吗?

我看到了下面的例子,但我不确定它的有效性:

(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];

}

【问题讨论】:

  • 需要指出这里最重要的一行是[toolbar sizeToFit];没有它 - 显示工具栏,但不接受任何用户交互

标签: iphone ios uitableview uitoolbar


【解决方案1】:

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

在您的应用委托中,或在您的应用委托传递控制权的对象中,使用您的 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 ];
}

然后在您的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 ... */
}

【讨论】:

  • 这是一个了不起的建议。我从来不知道 UINavigationController 有一个默认隐藏的工具栏。我已经在使用它了,所以这是一个真正的奖励。也感谢您花时间真正解释一切。
【解决方案2】:

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

【讨论】:

  • 但是它会在所有视图上打开,最终在那些不应该使用工具栏的视图上使用代码。
【解决方案3】:

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

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];

谢谢, 开发者

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多