我是IOS的新手,
我想补充一UINavigationBar
到UITableViewController
,我已经试过这样:
var navBar: UINavigationBar = UINavigationBar(frame: CGRect(x:0, y:0, width:320, height:80))
Run Code Online (Sandbox Code Playgroud)
然后,
self.view .addSubview(navBar)
Run Code Online (Sandbox Code Playgroud)
谢谢
Ric*_*cky 15
你不能simplly添加NavigationBar
到UITableViewController
这样.
从Storyboard中获取UINavigationController
和使用NavigationBar
它的最简单方法.
脚步:-
将UITableViewController对象从对象库拖到Storyboard.
突出显示UITableViewController,转到编辑 - >嵌入 - >导航控制器,如下面的屏幕截图所示: -
转到文件 - >新建 - >文件.. - > iOS - > Cocoa Touch Class,并创建一个自定义TableViewController类,如下面的屏幕截图: -
您可以使用自定义类文件执行任何操作.您也可以添加自定义UINavigationController
类,以及如果你想和你可以将自定义类在故事板内的对象.
如果这仅仅是呈现出的情况下,模式 UITableViewController
与一个导航栏,从代码中做到这一点,最简单的方法是提供一个UINavigationController
与你UITableViewController
为rootViewController
:
呈现视图控制器:
let sourceSelectorTableViewController = SourceSelectorTableViewController()
let navigationController = UINavigationController(rootViewController: sourceSelectorTableViewController)
self.presentViewController(navigationController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
目标模态UITableViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "done")
self.title = "Pick a Source"
}
func cancel() {
self.dismissViewControllerAnimated(true, completion: nil)
}
func done() {
//save things
self.dismissViewControllerAnimated(true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17846 次 |
最近记录: |