【发布时间】:2016-06-09 02:54:11
【问题描述】:
我有一个带有 UITableView 的 UIViewController(不是 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后面?你在使用视图层次检查器吗?
-
是的,我正在使用层次结构检查器,我的浮动按钮始终位于视图层次结构中,但位于 tableview 后面
-
确实我使用的是 LiquidFloatingActionButton 框架,我的方法 .addActionButton() 引用了一个与示例中相同的方法,只是将按钮(称为 bottomRightButton)作为视图返回
-
如何添加表格视图?
标签: swift uitableview uiview uiviewcontroller uikit