正如问题所述,我想在我的 View Controller 内的 TableView (底部)顶部添加一个 UIButton
。据我了解,一般情况下您不能在表格 View 之上添加任何 View ,或者至少根据我的尝试,我是这么认为的:
let shareButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
shareButton.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)
shareButton.backgroundColor = imageColors[currentTeam.homeName]
shareButton.tintColor = UIColor.whiteColor()
shareButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 16)
shareButton.setTitle("Send!", forState: UIControlState.Normal)
shareButton.addTarget(self, action: "send:", forControlEvents: UIControlEvents.TouchUpInside)
//Tried this
self.view.insertSubview(shareButton, aboveSubview: self.tableView)
//And this
self.view.addSubview(shareButton)
//Also tried making a toolbar. But that didn't work either
var toolbar: UIToolbar = UIToolbar()
toolbar.frame = CGRectMake(0, UIScreen.mainScreen().bounds.height - 50, self.view.frame.size.width, 50)
self.view.addSubview(toolbar)
最佳答案
我试过你的代码(使用 self.view.addSubview(shareButton))它的工作
如果你想看代码,我粘贴在这里..
override func viewDidLoad() {
super.viewDidLoad()
let shareButton = UIButton()
shareButton.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)
shareButton.backgroundColor = UIColor.redColor()
shareButton.tintColor = UIColor.whiteColor()
shareButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 16)
shareButton.setTitle("Send!", forState: UIControlState.Normal)
shareButton.addTarget(self, action: "send:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(shareButton)
data = ["Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay","Ajay","Ajay","AJay"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "cell")
}
cell!.selectionStyle = .None
cell!.textLabel?.text = data![indexPath.row]
return cell!
}
关于ios - 添加 UIButton 以覆盖在 UITableView 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478095/