【问题标题】:How to keep toolbar in place under navigation bar如何将工具栏保持在导航栏下的位置
【发布时间】:2016-11-12 04:02:19
【问题描述】:

我在导航控制器中嵌入了一个 UITableViewController。我在导航栏下放了一个工具栏来扩展它。它看起来像这样:

但是,当我向上滑动以查看更多表格视图单元格时,工具栏会滑到导航栏后面并消失。当我移动表格单元格时,有没有办法将它永久附加在导航栏下而不移动?

【问题讨论】:

  • 你是怎么把工具栏放在导航栏下面的?它是表格的一部分吗?
  • 是的,我把它挤在了 tableView 里
  • 喜欢标题还是行?
  • 导航栏正下方,“原型单元格”正上方

标签: ios xcode swift uinavigationcontroller uitoolbar


【解决方案1】:

如果您将子视图添加到UITableViewController,它将随其内容滚动。 UITableViewControllerViewController 的一种特定类型,可显示全屏表格。

要将另一个子视图添加为固定标题,您可以创建一个自定义 UIViewController,其中有一个 UITableView 作为子视图。然后实现UITableViewDataSourceUITableViewDelegate 协议以获得UITableViewController 的功能。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // change to data
        return 5;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // can also use custom cell from xib
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("default");

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "default");
        }

        cell!.textLabel!.text = "example";

        return cell!;
    }

}

还值得注意的是,如果您计划在情节提要中的多个UIViewControllers 中使用此工具栏,您应该使用容器视图来指定和重复使用它的设计。

【讨论】:

    【解决方案2】:

    将您的表格样式设置为 UITableViewStyleGrouped 并将您的工具栏添加为部分标题,使用:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        return _yourtoolbar;
    }
    

    【讨论】:

    • 这个问题被标记为“swift”。您的解决方案可能有价值,但不是以 OP 要求的语言编写的 - 请用 Swift 提供答案。谢谢。
    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多