ios - 添加 UIButton 以覆盖在 UITableView 之上

标签 ios swift

正如问题所述,我想在我的 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)

我想要实现的效果类似于snapchat的效果: what i want

最佳答案

我试过你的代码(使用 self.view.addSubview(shareButton))它的工作

Screen shot

如果你想看代码,我粘贴在这里..

 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/

相关文章:

ios - 从 GCDWebServer 获取 Bonjour URL

json - Swift 可编码字典

ios - 换行后如何在 UITextView 中获取行?

ios - presentViewController 不显示呈现 View Controller 上的所有元素

iphone - 选中时突出显示单元格的一部分

ios - 将字典中的值分配给结构

audio - Swift:合并音频和视频文件

swift - 由于 Firebase,MKUserTrackingButton 没有出现?

ios - UISwitch 不应该以相反的方式工作吗?

objective-c - 异步 NSURLConnection 与 NSOperation