【问题标题】:UIView is always behind UITableViewUIView 总是落后于 UITableView
【发布时间】:2016-06-09 02:54:11
【问题描述】:

我有一个带有 UITableViewUIViewController不是 UITableViewController,因为我读到视图控制器最适合这种行为)。在这个视图控制器中,我想添加一个浮动的 UIView 并将其放在我的 tableview 上方,为此我写道:

    public override func viewDidLoad() {
    super.viewDidLoad()

    // add button
    let fbv = liquidActionButtonInstance.addActionButton() // this is a UIView
    self.view.addSubview(fbv)
    liquidActionButtonInstance.delegate = self

    // delegate
    tableView.delegate = self
    tableView.dataSource = self
}

但是我的浮动视图出现在我的UITableView 后面,我怎样才能将它添加为self.view 的第一个孩子?我用过

self.view.addSubview(fbv)
self.view.bringSubviewToFront(fbv)

除其他外,似乎没有一个工作。

编辑:

我添加了一些视图层次结构的屏幕截图。

编辑 2:

这里我放了一点代码: 我的 ViewController 没有一些不相关的代码:

    public class ActividadesTableViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var descripcionFiltrosLabel: UILabel!
    @IBOutlet weak var filtrosLabelBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var filtrosLabelTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var tableViewTopConstraint: NSLayoutConstraint!

    private var liquidActionButtonInstance: FloatingActionButton = FloatingActionButton()

    public var viewModel : ActividadesTableViewModeling?

    public override func viewDidLoad() {
        super.viewDidLoad()

        // Agregar action button
        self.view.insertSubview(liquidActionButtonInstance.addActionButton(),aboveSubview: tableView)
        liquidActionButtonInstance.delegate = self

        // set row's height
        tableView.estimatedRowHeight = 70
        tableView.rowHeight = UITableViewAutomaticDimension

        // delegate
        tableView.delegate = self
        tableView.dataSource = self

        // load tableview data
        if let viewModel = viewModel {
            viewModel.loadActividades(withFilters: nil)
        }
    }
}

// MARK: FloatingButton

extension ActividadesTableViewController: FloatingActionButtonDelegate {
    public func performSegueFromFloatingActionButton(segueName name: String) {
        performSegueWithIdentifier(name, sender: self)
    }
}

// MARK: TableView

extension ActividadesTableViewController: UITableViewDataSource, UITableViewDelegate {
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Omitting this code, just mentioning the methods
    }

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ActividadCell", forIndexPath: indexPath) as! ActividadTableViewCell
        if let viewModel = viewModel {
            cell.viewModel = viewModel.cellModels.value[indexPath.row]
        } else {
            cell.viewModel = nil
        }

        return cell
    }

    public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    }
}

这就是我添加按钮的方式(在不同的类中):

    func addActionButton() -> LiquidFloatingActionButton {
    let createButton: (CGRect, LiquidFloatingActionButtonAnimateStyle) -> LiquidFloatingActionButton = { (frame, style) in
        let floatingActionButton = LiquidFloatingActionButton(frame: frame)
        floatingActionButton.animateStyle = style
        floatingActionButton.dataSource = self
        floatingActionButton.delegate = self
        floatingActionButton.color = ColoresKairos.principal2
        return floatingActionButton
    }

    let cellFactory: (String) -> LiquidFloatingCell = { (iconName) in
        return LiquidFloatingCell(icon: UIImage(named: iconName)!)
    }
    cells.append(cellFactory("iphone-action-button-group"))
    cells.append(cellFactory("iphone-action-button-notepad"))
    cells.append(cellFactory("iphone-action-button-check-box"))

    let floatingFrame = CGRect(x: UIScreen.mainScreen().bounds.width - 56 - 16, y: UIScreen.mainScreen().bounds.height - 56 - 16, width: 56, height: 56)
    let bottomRightButton = createButton(floatingFrame, .Up)

    //view.addSubview(bottomRightButton)
    //return view

    return bottomRightButton
}

【问题讨论】:

  • 你怎么知道它在你的tableView后面?你在使用视图层次检查器吗?
  • 好像你正在使用this 框架。您可以尝试复制他们在 viewDidLoad here 中所做的事情。
  • 是的,我正在使用层次结构检查器,我的浮动按钮始终位于视图层次结构中,但位于 tableview 后面
  • 确实我使用的是 LiquidFloatingActionButton 框架,我的方法 .addActionButton() 引用了一个与示例中相同的方法,只是将按钮(称为 bottomRightButton)作为视图返回
  • 如何添加表格视图?

标签: swift uitableview uiview uiviewcontroller uikit


【解决方案1】:

您需要使用addSubview(_:) AND bringToFront(_:) 添加视图。你也可以尝试使用sendToBack(_:)将tableView发送到后面

【讨论】:

  • 我尝试使用self.view.addSubview(fbv),然后是self.view.bringSubviewToFront(fbv),最后是self.view.sendSubviewToBack(tableView),但我的按钮仍在我的tableview背面
【解决方案2】:

在情节提要的文档大纲菜单中,您可以将浮动视图放置在视图层次结构中的 TableView 下方。然后,您始终可以看到浮动视图位于 tableview 上方。

会是这样的。

▼ 你的视图控制器

顶部布局指南

底部布局指南

▼查看

▶︎ 表格视图

▶︎浮动视图

【讨论】:

  • 但是如果我在我的故事板中添加一个 UIView 来放置我的新按钮,我如何与它下面的 tableview 交互?我需要同时访问表格视图的单元格和浮动按钮中的操作。
  • 您创建了一个从按钮到 viewController 的出口,然后您可以像在代码中创建它一样访问它(但故事板已为您准备好按钮)。你仍然可以做任何事情。
【解决方案3】:

以下是与其屏幕截图一起工作的代码,我怀疑您的调用 liquidActionButtonInstance.addActionButton() 返回一个空按钮?

class ViewController: UIViewController {

   @IBOutlet weak var tableView: UITableView!
   private var liquidButton: LiquidFloatingActionButton?

   override func viewDidLoad() {
     super.viewDidLoad()

     tableView.delegate = self
     tableView.dataSource = self

     // Setting up the liquid button
     liquidButton = createLiquidButton()
     view.addSubview(liquidButton!)
   }

   func createLiquidButton() -> LiquidFloatingActionButton {
     let frame = CGRect(x: UIScreen.mainScreen().bounds.width - 56 - 16, y: UIScreen.mainScreen().bounds.height - 56 - 16, width: 56, height: 56)

     let button = LiquidFloatingActionButton(frame: frame)
     button.animateStyle = .Up
     button.color = UIColor.redColor()

     return button
  }

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
  // UITableView Implementation, not included not relevent for the example.
}

【讨论】:

    【解决方案4】:

    这里的错误是,在调试视图层次结构后,我的 tableView 没有直接添加到 UIViewController 的层次结构中,因此它被添加到其他所有内容之上,并且将我的 FloatingButton 添加为 UIViewController 的子视图总是导致它被隐藏通过我的表视图。 我的 tableView 是通过情节提要添加的,据我所知,这不是预期的行为,但在我的情况下,只需添加以下几行:

    self.view.addSubview(tableView)
        self.view.addSubview(liquidActionButtonInstance.addActionButton())
    

    解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多