从 xcode 故事板可视化修改 UIToolbar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8707540/
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-09-14 22:52:28  来源:igfitidea点击:

Visually modifying a UIToolbar from xcode storyboard

iosxcodexcode4ios5

提问by 1dayitwillmake

I'm using XCode 4 and storyboarding an application I'm creating, however I cannot visually modify the UIToolbar.

我正在使用 XCode 4 并为我正在创建的应用程序制作故事板,但是我无法直观地修改 UIToolbar。

I'm attempting to modify a UIToolbar that is inside of a UITableViewController - however I have to place the UIToolbar out of the correct hierarchy in order to be able to modify it visually. I've tried placing it onto the view controller area but that does not make it show up.

我正在尝试修改 UITableViewController 内的 UIToolbar - 但是我必须将 UIToolbar 放在正确的层次结构之外,以便能够在视觉上对其进行修改。我试过把它放在视图控制器区域,但这并没有让它显示出来。

At one point I was able to make it appear below, as it's own object however I was not able to recreate that.

有一次我能够让它出现在下面,因为它是自己的对象,但是我无法重新创建它。

Problem

问题

Once I was able to get it to look like this Problem

一旦我能够让它看起来像这样 问题

回答by Ryder Mackay

Your UITableViewControlleris inside a UINavigationController, which already has its own UIToolbar—you don't need to drag a new one into your view hierarchy. Interface Builder can simulate the toolbar for you under "Simulated Metrics" in the inspector panel.

UITableViewController在 a 中UINavigationController,它已经有了自己的UIToolbar——您不需要将一个新的拖到您的视图层次结构中。Interface Builder 可以在检查器面板的“模拟指标”下为您模拟工具栏。

Once the simulated toolbar is visible, simply drag UIBarButtonItems into it. Use a custom item with a custom view if you need anything more complicated than a button or spacer.

一旦模拟工具栏可见,只需将UIBarButtonItems 拖入其中即可。如果您需要比按钮或垫片更复杂的东西,请使用带有自定义视图的自定义项目。

If you need your toolbar items need to be dynamic, you can maintain a reference via IBOutlets without necessarily having them in your view. Then set your UITableViewController's toolbarItemsproperty or call -setToolbarItems:animated:at runtime to display the appropriate items.

如果你需要你的工具栏项目需要是动态的,你可以通过IBOutlets维护一个引用,而不必在你的视图中看到它们。然后设置您UITableViewControllertoolbarItems属性或-setToolbarItems:animated:在运行时调用以显示适当的项目。

See Apple's UINavigationControllerClass Reference: Displaying a Toolbar.

请参阅 Apple 的UINavigationController类参考:显示工具栏

回答by Justin

To answer your question, the visual editor simplifies the setup of most controls, view hierarchies, and delegation patterns, but it's still up to the developer to make sure they check out. The implementation of UITableViewControllermakes certain assumptions and assertions about its view hierarchy that Xcode does not enforce through the visual editor. Given that your desired view hierarchy is unsupported, I have to assume that the editor's behavior is either undefined or irrelevant. For a workaround, see Maxner's suggestion.

为了回答您的问题,可视化编辑器简化了大多数控件、视图层次结构和委派模式的设置,但仍由开发人员来确保他们签出。的实现UITableViewController对其视图层次结构做出了某些假设和断言,Xcode 不会通过可视化编辑器强制执行这些假设和断言。鉴于不支持您想要的视图层次结构,我必须假设编辑器的行为未定义或不相关。有关解决方法,请参阅 Maxner 的建议。

回答by JonasG

UITableViewControllers only allow one view object, which of course is UITableView. UITableViews are not cooperative for subviewing and they usually push everything into footers or headers. Something like this isn't possible:

UITableViewControllers 只允许一个视图对象,当然是 UITableView。UITableViews 不合作进行子查看,它们通常将所有内容推送到页脚或页眉中。像这样的事情是不可能的:

-TableController
    -Table
        -Subview
        -Another subview

UITableViewControllers are reduced to this:

UITableViewControllers 简化为:

-TableViewController
    -Table

So you will need to use a UIViewController and declare a UITableView in there. Heres the Hierarchy you should use then:

所以你需要使用一个 UIViewController 并在那里声明一个 UITableView 。这是您应该使用的层次结构:

- ViewController <Table's Delegate & Data Source>
    - View           
        -Table
        - Your UIToolbar

In your ViewController.h declare IBOutlet UITableView and connect Data Source and Delegate to the ViewController. Then simply add the UITableView implementations to your .m file and you're good to go.

在您的 ViewController.h 中声明 IBOutlet UITableView 并将数据源和委托连接到 ViewController。然后只需将 UITableView 实现添加到您的 .m 文件中,您就可以开始了。