如何使用Swift向UITableViewController显示和添加工具栏按钮
我正在尝试通过编程方式显示嵌入UINavigationController中的UITableViewController中的工具栏,并将工具栏按钮添加到工具栏中。此时将显示工具栏,但不显示任何按钮。这是我的密码:如何使用Swift向UITableViewController显示和添加工具栏按钮,swift,uinavigationcontroller,uitabbarcontroller,uitoolbar,Swift,Uinavigationcontroller,Uitabbarcontroller,Uitoolbar,我正在尝试通过编程方式显示嵌入UINavigationController中的UITableViewController中的工具栏,并将工具栏按钮添加到工具栏中。此时将显示工具栏,但不显示任何按钮。这是我的密码: var items = [UIBarButtonItem]() items.append(UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)) i
var items = [UIBarButtonItem]()
items.append(UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil))
items.append(UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("btnClearAction")))
self.navigationController!.setToolbarHidden(false, animated: true)
self.navigationController!.setToolbarItems(items, animated: false)
在将我的应用程序迁移到swift时,我刚刚发现我也遇到了同样的问题。我只是想把它添加到一个左菜单控制器中,这个控制器保存一个表视图。不应该有那么大的不同吗 我刚刚通过编程创建工具栏解决了这个问题 以下是我所做的: 下课
class LeftViewController: ViewController, UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView!
var toolBar: UIToolbar!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 45/255, green: 45/255, blue: 45/255, alpha: 1.0)
self.tableView = UITableView(frame: self.view.bounds, style: .Plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
//UPDATE #3
self.tableView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)
self.edgesForExtendedLayout = .None
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBarHidden = true
let Button1 = UIBarButtonItem(title: "Button1", style: .Plain, target: self, action: "your action")
let Button2 = UIBarButtonItem(title: "Button2", style: .Plain, target: self, action: "your action")
let Button3 = UIBarButtonItem(title: "Button3", style: .Plain, target: self, action: "your action")
let Button4 = UIBarButtonItem(title: "Button4", style: .Plain, target: self, action: "your action")
//Flexiable Space
let flexiableItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
//UPDATE #2
toolbarItems = [Button1, Button2, flexiableItem, Button3, Button4]
}
很明显,你的按钮、视图、单元格等的样式都是你想要的
我确实遗漏了很多我发布的内容,因为它与主题无关
希望这有帮助
更新:
令人惊叹的。一个修复带来另一个问题。
旋转设备时,我的工具栏消失。向后旋转,按钮就不见了。可能只是我项目的一部分,但耶
更新#2:
使用工作代码更新答案。删除了我制作的工具栏,找到了一个有效的解决方案。即使旋转
更新#3:
将表格视图设置为20px,创建“状态栏”,并将表格视图底部缩小20px,使其位于工具栏顶部。Thx但导航控制器内置了工具栏,因此我认为此解决方案没有意义。如果您碰巧阅读了UPDATE#2,您将看到,我删除了我创建的工具栏,并使用了标准工具栏项。指向导航控制器工具栏。抱歉。我没看到。谢谢。我会在火鸡日后退房。