简体   繁体   English

将任意定位的按钮添加到UITableViewController

[英]Add arbitrarly positioned button to a UITableViewController

I have a UITableViewController to which I want to add a button at the bottom right corner of. 我有一个UITableViewController,我想在该UITableViewController的右下角添加一个按钮。 The button would stay there as you scroll and just be in front of the table view. 当您滚动时,该按钮将停留在该位置,并且位于表格视图的前面。

How would I add a button like this? 我如何添加这样的按钮? I've tried adding a transparent subview with the button but the only thing it changes is removing the separators of my tableview (heres how I tried it, I added this to the viewDidLoad ) 我尝试使用按钮添加一个透明的子视图,但是它唯一改变的是删除表视图的分隔符(这是我尝试的方式,将其添加到viewDidLoad

let transparentView = UIView(frame: self.view.frame)
transparentView.alpha = 0

let plusButton = UIButton(frame: CGRectMake(50, 50, 50, 50))
plusButton.imageView?.image = UIImage(named: "plus")
transparentView.addSubview(plusButton)
self.view.addSubview(transparentView)

In storyboard, you can add the button above the UITableView, and position it with constraints. 在情节提要中,您可以在UITableView上方添加按钮,并将其置于约束中。 You can set an image and all that stuff there. 您可以在那里设置图像和所有其他东西。

If you need to do it in code, then just add the button as a subview to your UITableViewController. 如果您需要在代码中执行此操作,则只需将按钮作为子视图添加到UITableViewController。 If you put it in another view and set the alpha to 0 then the button disappears with it's superview. 如果将其放在另一个视图中并将alpha设置为0,则该按钮将消失,并成为其超级视图。 You can use something like PureLayout to keep it in the corner. 您可以使用诸如PureLayout之类的东西将其放置在角落。

let plusButton = UIButton(frame: CGRectMake(50, 50, 50, 50))
plusButton.imageView?.image = UIImage(named: "plus")
self.view.addSubview(plusButton)
plusButton.autoPinEdgeToSuperviewEdge(ALEdge.Right, withInset: 8)
plusButton.autoPinEdgeToSuperviewEdge(ALEdge.Bottom, withInset: 8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM