xcode presentModalViewController 不会出现顶栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7265245/
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
Top Bar does not appear for presentModalViewController
提问by 5StringRyan
I've created a UIViewController subclass called addItemToListViewController. I selected add an "xib" as well, and just created a simple page with a couple of labels and a textField. In the interface builder I selected "Top Bar - Navigation Bar" so that when it is put on the stack when the application runs it will have a top bar that will match the initial main window. In the Interface builder it shows the top border, but when I run the application in the simulator the top bar is not present once the view is displayed.
我创建了一个名为 addItemToListViewController 的 UIViewController 子类。我也选择添加一个“xib”,并创建了一个带有几个标签和一个 textField 的简单页面。在界面构建器中,我选择了“顶部栏 - 导航栏”,以便在应用程序运行时将它放在堆栈上时,它将有一个与初始主窗口匹配的顶部栏。在界面构建器中,它显示了顶部边框,但是当我在模拟器中运行应用程序时,一旦显示视图,顶部栏就不存在了。
Here is the code I placed in the rootViewController to present the view controller
这是我放在 rootViewController 中的代码来呈现视图控制器
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: @"AddItemToListViewController" bundle:nil];
[self presentModalViewController: AddItemToListViewController animated: YES];
[AddItemToListViewController release];
}
I'm only able to have the top bar present if I manually add a Navigation bar to the xib. If I must add a Navigation bar to my xib, what is the purpose of the "Top Bar" attribute?
如果我手动将导航栏添加到 xib,我只能显示顶部栏。如果我必须向我的 xib 添加导航栏,“顶栏”属性的目的是什么?
回答by Robin
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: @"AddItemToListViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addItemToListViewController];
[self presentModalViewController: navController animated: YES];
[AddItemToListViewController release];
[navController release];
}
回答by AndrewS
That "top bar - Navigation bar" in InterfaceBuilder is what's known as a "Simulated Metric". It's there to help you lay out your view with correct spacing when other visual elements - the status bar, navigation bar, or tab bar - might consume some of the device's screen real estate. It doesn't actually do anything other than shrink the vertical dimensions of the view defined by the NIB. The purpose is to help you layout your view, not to actually createa component that will appear in your app.
InterfaceBuilder 中的“顶部栏 - 导航栏”就是所谓的“模拟指标”。当其他视觉元素(状态栏、导航栏或标签栏)可能占用设备的某些屏幕空间时,它可以帮助您以正确的间距布置视图。除了缩小由 NIB 定义的视图的垂直尺寸之外,它实际上并没有做任何事情。目的是帮助您布局视图,而不是实际创建将出现在您的应用程序中的组件。
If you want a navigation bar, then you have two choices. The first choice is to use a navigation controller (of which your initial view will have to be the root) and call
如果你想要一个导航栏,那么你有两个选择。第一个选择是使用导航控制器(您的初始视图必须是根)并调用
[self.navigationController pushViewController:newVC animated:YES];
The process of setting up a navigation controller correctly, etc, is nontrivial, and you should do some searching to find the best way to do that for your app. For a simple app, especially if you're just learning iOS, you can use the "Navigation-based Application" template when you create a new project. With a navcon, you get all the fancy behavior normally associated with that top bar - an automatic back button, fancy left/right scrolling when you transition to a detail view, etc.
正确设置导航控制器等的过程非常重要,您应该进行一些搜索以找到为您的应用程序执行此操作的最佳方法。对于一个简单的应用程序,特别是如果您刚刚学习iOS,您可以在创建新项目时使用“基于导航的应用程序”模板。使用导航图标,您可以获得通常与顶部栏相关联的所有奇特行为 - 自动后退按钮、转换到详细信息视图时的奇特左/右滚动等。
The second option is to put a "fake" navigation bar in the detail view, using the Navigation Bar object. You can find that object, plus some other related objects, in the bottom half of the "Utilities View" (the right-most pane) in XCode. Just drag the object into your XIB and blammo, you have a 44-pixel tall gray bar. This navigation bar is just like what you get when you use a Navigation Controller except you don't get the stack functionality; you can still add buttons to the left and right, change the title, tint it to a specific color, etc.
第二个选项是使用导航栏对象在详细信息视图中放置一个“假”导航栏。您可以在 XCode 的“实用程序视图”(最右侧的窗格)的下半部分找到该对象以及其他一些相关对象。只需将对象拖到您的 XIB 和 blammo 中,您就会有一个 44 像素高的灰色条。这个导航栏就像你使用导航控制器时得到的一样,只是你没有获得堆栈功能;您仍然可以向左右添加按钮、更改标题、将其着色为特定颜色等。
回答by Manuel
The xib does not know you will use the controller as a modal view as it could also be used for a normal view which could show a top bar. Only when you push the view it will use or ignore the showing of this top bar.
xib 不知道您将使用控制器作为模态视图,因为它也可以用于可以显示顶部栏的普通视图。只有当您推动视图时,它才会使用或忽略此顶栏的显示。
In short: its there in case you will use the xib for a normal view :)
简而言之:它在那里,以防您将 xib 用于普通视图:)