ios 如何在 Interface Builder 中向 UITableViewController 添加导航栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5068659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:58:15  来源:igfitidea点击:

How do I add a Navigation Bar to a UITableViewController in Interface Builder?

iphoneiosinterface-builderuitableviewuinavigationbar

提问by rustybeanstalk

Interface builder does not let me click and drag a Navigation Bar onto a Table View Controller!!! It is super frustrating.

界面生成器不允许我单击导航栏并将其拖到表视图控制器上!!!这是超级令人沮丧。

All I want is a table view with an edit button (done in interface-builder). If this is not possible, then how do I add a navbar progammatically?

我想要的只是一个带有编辑按钮的表格视图(在界面构建器中完成)。如果这是不可能的,那么如何以编程方式添加导航栏?

Thanks in advance.

提前致谢。

回答by Greeso

From the outline view, make sure your Table View Controlleris selected.

在大纲视图中,确保选择了您的表视图控制器

Then go to the Editormenu, and click on the Embed Insubmenu, and choose Navigation Controllerand voila. You have your navigation controller pointing to your tableview controller with a relationship built in.

然后转到 Editor菜单,单击Embed In子菜单,然后选择Navigation Controller,瞧。您将导航控制器指向具有内置关系的 tableview 控制器。

回答by stephenhow

For a table view with an edit button at the top, use a UINavigationController, with a UITableView as the rootView. That means you're going to make a custom UITableView subclass for your table view, and use that as the rootView of your UINavigationController instance. (Programatically, it's set with UINavigationController's -(id)initWithRootViewController. It's also settable through IB.)

对于顶部带有编辑按钮的表视图,请使用 UINavigationController,将 UITableView 作为 rootView。这意味着您将为您的表格视图创建一个自定义 UITableView 子类,并将其用作您的 UINavigationController 实例的 rootView。(以编程方式,它是用 UINavigationController's 设置的-(id)initWithRootViewController。它也可以通过 IB 设置。)

Then, in your UITableView subclass, uncomment the following line:

然后,在您的 UITableView 子类中,取消注释以下行:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

and voilà, your UINavigationController's view shows up as a table view with an edit button on the right side of the navigation bar.

瞧,你的 UINavigationController 的视图显示为一个表格视图,在导航栏的右侧有一个编辑按钮。

Since the controller is at the top of the stack, there's no "back" button on the left, so you can use self.navigationItem.leftBarButtonItemfor whatever UIBarButtonItem you create.

由于控制器位于堆栈顶部,因此左侧没有“后退”按钮,因此您可以将其self.navigationItem.leftBarButtonItem用于您创建的任何 UIBarButtonItem。

回答by jlehr

I agree that it's difficult to figure out how to do things like this in Interface Builder, but luckily it is possible to add a Navigation Bar and Bar Button Item to a Table View this way. Here's how to do it:

我同意在 Interface Builder 中很难弄清楚如何做这样的事情,但幸运的是,可以通过这种方式将导航栏和栏按钮项添加到表格视图中。这是如何做到的:

  1. Drag a blank View (an instance of UIView) from the Library to the area near the top of the Table View. As you drag near the target area, Interface Builder will highlight it in blue to show you where to drop the View. Let go, and the View will be added as a subview of the Table View's header view.
  2. Drag a Navigation Bar from the Library and drop it on the blank View you just added.
  3. Drag a Bar Button Item from the Library and drop it onto the Navigation Bar.
  1. 将一个空白视图(UIView 的一个实例)从库中拖到靠近表视图顶部的区域。当您在目标区域附近拖动时,Interface Builder 将以蓝色突出显示它以显示放置视图的位置。放手,View 将被添加为 Table View 的标题视图的子视图。
  2. 将导航栏从库中拖放到您刚刚添加的空白视图上。
  3. 从库中拖动一个栏按钮项并将其放到导航栏上。

EDIT

编辑

The problem with the above approach is that, as Bogatyr points out, the Navigation Bar will then scroll along with the Table View. Apple recommends using a custom subclass of UIViewControllerthat owns both the Navigation Bar and an instance of UITableViewresized to fit. Unfortunately, that means you would have to implement the UITableViewControllerbehavior needed by your UIViewControllersubclass yourself.

上述方法的问题在于,正如 Bogatyr 指出的那样,导航栏将随表视图一起滚动。Apple 建议使用UIViewController拥有导航栏和UITableView调整大小以适应的实例的自定义子类。不幸的是,这意味着您必须自己实现子类UITableViewController所需的行为UIViewController

Another approach that seems to work well is to create a custom subclass of UIViewControllerthat owns a blank background view containing the Navigation Bar as well as a blank content view (an instance of UIView) that fits under the Navigation Bar. Your custom subclass would have an outlet pointing to an instance of UITableViewControllerin the same nib file.

另一种似乎工作良好的方法是创建一个自定义子类,UIViewController该子类拥有包含导航栏的空白背景视图以及UIView适合导航栏下方的空白内容视图( 的实例)。您的自定义子类将有一个指向UITableViewController同一个 nib 文件中的实例的插座。

This has the advantage of allowing all the view components to be created and configured in Interface Builder, and doesn't require implementing UITableViewControllermethods from scratch. The only detail you'd need to take care of in the Table View Controller's parent would be to add Table View as a subview of the parent's content view in viewDidLoad.

这具有允许在 Interface Builder 中创建和配置所有视图组件的优点,并且不需要UITableViewController从头开始实现方法。您需要在 Table View Controller 的父级中处理的唯一细节是将 Table View 添加为viewDidLoad.

The parent could implement the action methods for the Navigation Bar's button items, and implement the delegate pattern if necessary.

父级可以实现导航栏按钮项的操作方法,并在必要时实现委托模式。

回答by cocoaNoob

From iOS6 onwards, you can use container view. So what you have to do is take View controller, add the navigation bar to it, then add a Container View to same view controller. It will automatically, add the new view controller link to your container view. Now simply delete that, and your table view controller in the story board. Now embed the table view controller to container view by control drag. Hope it helps.

从 iOS6 开始,您可以使用容器视图。所以你要做的就是把浏览器,导航栏添加到它,然后添加一个容器视图,以相同的视图控制器。它会自动将新的视图控制器链接添加到您的容器视图。现在只需删除它,以及故事板中的表视图控制器。现在通过控件拖动将表视图控制器嵌入到容器视图中。希望能帮助到你。

回答by dasdom

First add a navigation controller and put the table view controller (as root view controller) onto the navigation controller. This is how it is done in Code because I don't use IB.

首先添加一个导航控制器并将表视图控制器(作为根视图控制器)放到导航控制器上。这是在代码中完成的方式,因为我不使用 IB。

回答by Bogatyr

Why in the world you can't drag a navigationItem into a .xib file with File's Owner set to a subclass of UIViewController and hook the navigationItem up to the UIViewController's navigationItem outlet is beyond me. It seems like a real hole in IB / XCode integration. Because you can certainly drag an instance of ViewController to a xib file, and drag a navigationItem into the ViewController, and then set the title and barbuttonitems that way.

为什么在世界上你不能将一个 navigationItem 拖到一个 .xib 文件中,File's Owner 设置为 UIViewController 的子类,并将 navigationItem 连接到 UIViewController 的 navigationItem 出口,这超出了我的理解。这似乎是 IB / XCode 集成中的一个真正漏洞。因为你当然可以拖一个ViewController的实例到xib文件,然后拖一个navigationItem到ViewController中,然后这样设置title和barbuttonitems。

So if you want to define your UITableViewController subclass object's navigation bar in IB, you have to create your TableVC object in a xib file (not the one .xib file that contains the tableview for your UITableViewController, though!). You then either hook the TableVC object up to be an outlet of another object (like your application delegate), which works if you need just one instance of your TVC throughout the lifetime of your app, or if you want to dynamically create instances of your TableVC in code you load this extra .xib file manually via loadNibNamed:owner:options method of the NSBundle class.

所以如果你想在 IB 中定义你的 UITableViewController 子类对象的导航栏,你必须在一个 xib 文件中创建你的 TableVC 对象(而不是一个包含 UITableViewController 的 tableview 的 .xib 文件!)。然后,您可以将 TableVC 对象连接到另一个对象(例如您的应用程序委托)的出口,如果您在应用程序的整个生命周期中只需要一个 TVC 实例,或者如果您想动态创建您的TableVC 在代码中,您可以通过 NSBundle 类的 loadNibNamed:owner:options 方法手动加载这个额外的 .xib 文件。

回答by hansengj

These steps worked for me in iOS 9:

这些步骤在 iOS 9 中对我有用:

  1. Add a View Controller to the Storyboard. Make UITableViewController as base Class.
  2. Add a Navigation Bar object onto view controller at the top.
  3. Add a Table View below Navigation bar.
  4. Add a Table View Cell into Table View.
  5. Add constraints.
  1. 将视图控制器添加到故事板。将 UITableViewController 作为基类。
  2. 在顶部的视图控制器上添加一个导航栏对象。
  3. 在导航栏下方添加一个表格视图。
  4. 将表视图单元格添加到表视图中。
  5. 添加约束。

回答by Ahmet ?entürk

This is the other easyway ;

这是另一种简单的方法;

  1. Choose your TableViewController screen on storyboard.
  2. Click Size Inspectorsymbol on the right menu and find Simulated Size
  3. Change Fixed to Free Form
  1. 在情节提要上选择您的 TableViewController 屏幕。
  2. 单击右侧菜单上的Size Inspector符号并找到Simulated Size
  3. 将固定更改为自由格式

You can add navigation bar easily.

您可以轻松添加导航栏。