为什么 [UIBarButtonItem setCustomView:] 方法禁用 Storyboard 中的 segue?
我的项目中有故事板。我使用 [UIBarButtonItem setCustomView:]
方法来自定义工具栏按钮。例如:
[self setCustomView:[[UIImageView alloc] initWithImage:image]];
现在它们看起来符合我的需要。但我发现这个方法以某种方式禁用了我为此工具栏项设置的segue。我的意思是,segue 没有它也可以工作,但是当我尝试使用此方法进行自定义时,segue 不起作用。但为什么?
我不想使用代码中的目标操作模式,我相信可以仅使用故事板。
I have Storyboard in my project. I use [UIBarButtonItem setCustomView:]
method to customize toolbar buttons. For example:
[self setCustomView:[[UIImageView alloc] initWithImage:image]];
Now they looks as I need. But I found that this method somehow disables segue that I set for this toolbar item. I mean, segue works without it, but when I tried to use this method for customization segue don't work. But why?
I don't want to use target-action pattern from code, I believe it is possible to use only Storyboard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直在尝试配置故事板中的所有 Segue,但我需要为 rightBarButtonItem 创建一个自定义视图。为了确保 Segue 仍然有效,只需在设置 customView 之前添加此行:
这样,您在 Storyboard 中设置的 Segue 仍将与 customView 一起触发。
我的整个自定义按钮代码如下所示:
I've been trying to configure all of the segues in the storyboard as well but I needed to create a custom view for the rightBarButtonItem. To make sure the segue still works, just add this line before setting the customView:
This way the segues you set up in your storyboard will still fire with customViews.
My entire custom button code looks like this:
我已经尝试过您所做的,但结果相同 - 图片是正确的,但 UIBarButtonItem 在按下时没有反应。可能这是一个错误。我的解决方法如下:
要在情节提要中执行此操作,请向工具栏添加一个 UIButton。您应该看到故事板通过将 UIButton 放入 UIBarButton 中来添加它。在 UIButton 上添加 segue。自定义 Storyboard 中的 UIButton。在我的应用程序中,我将大小设置为 40 x 40。然后在代码中使用视图自定义 UIButton。下面是将 imageView 添加到 UIButton 的代码示例:
注意:Storyboard 在向工具栏添加 UIButton 方面可能会很奇怪。看起来好像您在与导航控制器绑定的工具栏中执行此操作,但它不允许您添加它。我通过向情节提要添加一个虚拟视图控制器,向其中添加一个工具栏,然后将 UIButton 拖到该工具栏中来解决这个问题。 Storyboard 将通过将 UIButton 封装在 UIButtonBarItem 中来为您创建它。然后,您可以将其复制到项目中所需的工具栏并删除虚拟视图控制器。
还有其他方法可以做到这一点,例如在代码中创建按钮并将它们添加到工具栏。上面的方法最大限度地减少了代码。
I've tried what you have done with the same result - the picture is correct but the UIBarButtonItem doesn't react when pressed. Possibly this is a bug. My work around is as follows:
To do this in storyboard add a UIButton to the tool bar. You should see that storyboard adds it by putting the UIButton inside of the UIBarButton. Add the segue on the UIButton. Customize the UIButton in storyboard. In my App I set the size to 40 x 40. Then in your code customize the UIButton with the view. Here is an example of the code to add the imageView to the UIButton:
Note: Storyboard can be quirky about adding UIButtons to toolbars. It seems as if you do it in a toolbar that is tied to a navigation controller it won't let you add it. I've worked around this by adding a dummy view controller to the storyboard, adding a toolbar to that, then dragging the UIButton into that toolbar. Storyboard will create that for you by encapsulating the UIButton in a UIButtonBarItem. You can then copy then over to the the desired toolbar in your project and delete dummy view controller.
There are other ways to do this such as creating the buttons in code and adding them to the toolbar. The method above minimizes code.