【问题标题】:Add toolbar to UITableViewController将工具栏添加到 UITableViewController
【发布时间】:2010-12-02 08:19:13
【问题描述】:

将 UIToolBar 添加到 UITableViewController 的最简单方法是什么?我依赖于编辑功能,所以我不能轻易将 UITableViewController 更改为 UIViewController。

【问题讨论】:

    标签: iphone cocoa-touch uiviewcontroller uinavigationcontroller uitableview


    【解决方案1】:

    完全没问题,UITableViewControllerUIViewController 的子类。碰巧的是,在 iPhone OS 3.0 中,任何 UIViewController(和子类) 都可以与 UINavigationController 结合使用以提供上下文感知工具栏。

    为了使其工作,您必须:

    • 确保使用UINavigationController 来包含所有需要工具栏的视图控制器。
    • 设置需要工具栏的视图控制器的toolbarsItems 属性。

    这几乎就像设置视图控制器的标题一样简单,并且应该以相同的方式完成。很可能通过覆盖 initWithNibName:bundle: 初始化程序。举个例子:

    -(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
    {
      self = [super initWithNibName:name bundle:bundle];
      if (self) {
        self.title = @"My Title";
        NSArray* toolbarItems = [NSArray arrayWithObjects:
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                          target:self
                                                          action:@selector(addStuff:)],
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                                                          target:self
                                                          action:@selector(searchStuff:)],
            nil];
        [toolbarItems makeObjectsPerformSelector:@selector(release)];
        self.toolbarItems = toolbarItems;
        self.navigationController.toolbarHidden = NO;
      }
      return self;
    }
    

    您还可以使用setToolbarItems:animated: 而不是分配给toolbarItems 属性,以动态方式添加和删除工具栏项。

    【讨论】:

    • 是否需要 NavigationController?我想将 ToolBar 添加到不属于 NavigationController 的 TableViewController 中。即使只有一个视图,我是否需要使用 NavigationController?
    • @sirjorj 是的,UINavigationController 是获得 free 工具栏处理所必需的。没有它,您必须管理自己的 UIToolbar 视图实例。
    • 如果我不想在此工具栏中放置按钮,我只想在中心放置一个图像,我该怎么做?谢谢。
    【解决方案2】:

    为了使 PeyloW 的配方起作用,我需要添加以下额外的代码行:

    self.navigationController.toolbarHidden = NO;
    

    希望对您有所帮助...

    【讨论】:

    • 同意。我必须将该调用放在 viewDidLoad 方法中,而不是 initWithNibName 覆盖。然后效果很好。
    • 对我来说有效:self.navigationController?.isToolbarHidden = false;
    【解决方案3】:
    - (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];
    
        }
    

    在 Swift 3 中:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        //Initialize the toolbar
        let toolbar = UIToolbar()
        toolbar.barStyle = UIBarStyle.default
    
        //Set the toolbar to fit the width of the app.
        toolbar.sizeToFit()
    
        //Caclulate the height of the toolbar
        let toolbarHeight = toolbar.frame.size.height
    
        //Get the bounds of the parent view
        let rootViewBounds = self.parent?.view.bounds
    
        //Get the height of the parent view.
        let rootViewHeight = rootViewBounds?.height
    
        //Get the width of the parent view,
        let rootViewWidth = rootViewBounds?.width
    
        //Create a rectangle for the toolbar
        let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)
    
        //Reposition and resize the receiver
        toolbar.frame = rectArea
    
        //Create a button
        let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))
    
        toolbar.items = [infoButton]
    
        //Add the toolbar as a subview to the navigation controller.
        self.navigationController?.view.addSubview(toolbar)
    }
    
    func infoClicked() {
        //Handle Click Here
    }
    

    【讨论】:

    • 这对我很有用。我无法添加UINavigationController,因此手动添加工具栏是唯一的方法。谢谢!
    • 不错。我认为这应该是公认的答案。我想向 uitableviewcontroller 添加一个工具栏,而不是启用 uinavigationcontroller。
    • 这对我来说效果很好,但我没有添加UIBarButtonItems,而是添加了一个带有故事板按钮的视图到工具栏。当由 Xcode 7.2.1 编译时,它适用于 iOS11.4.1 之前的设备(未在 iOS12 上尝试过)。然后当我开始使用 Xcode 9.2 和 Xcode 10 时,该按钮在运行 iOS11+ 的设备或模拟器上没有响应(但在运行 iOS9 和 10 的模拟器上没有响应)。我想出的“解决方案”是在viewDidAppear 中删除并重新创建工具栏(再次)。在 viewDidAppear 而不是在 viewWillAppear 中创建工具栏不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 2016-09-05
    • 2013-07-25
    相关资源
    最近更新 更多