uinavigationController 的工具栏与自定义项目
我正在尝试在我的应用程序中使用 NavigationController 的工具栏。该工具栏的toolbarItems 应该根据呈现的视图控制器而变化。这是非常基本的。
我想做的是使用 UIBarButtonItem 的“initWithCustomView:”方法将自定义按钮添加到工具栏。但是,该按钮不会显示在工具栏上。但是,如果我使用“initWithTitle:”或“initWithBarButtonSystemItem:”方法创建 UIBarButtonItem,则会显示该按钮。例如,请查看下面的代码:
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil];
[self setToolbarItems:array];
如果完成此操作,按钮将显示在工具栏上。但是,如果我执行以下操作:
UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
replyBtn.frame = CGRectMake(10, 0, 40, 40);
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil];
[self setToolbarItems:array];
在此代码中,工具栏上仅显示 item1 和 item2。回复按钮未显示在工具栏上。按钮应该所在的位置有空白。
如果在我创建的常规工具栏而不是 NavigationController 的工具栏上使用相同的代码,则会显示该按钮。我试图在整个应用程序中只使用一个工具栏,以获得与苹果邮件应用程序相同的感觉。我需要使用“initWithCustomView:”方法的原因是因为其中一个图标是彩色的,这是它在普通工具栏上显示彩色的唯一方式。现在,我浏览了苹果文档,没有提到为什么无法调用“initWithCustomView:”方法(或者也许我找不到它)。
请有人对这个主题进行一些说明,以帮助我指出正确的方向。提前谢谢你们。
I am trying to use the NavigationController's toolbar in my app. This toolbar's toolbarItems are suppose to change depending on which view controller is presented. this is very basic.
What I am trying to do is to add custom buttons to the toolbar using the UIBarButtonItem's "initWithCustomView:" method. However, the button won't show up on the toolbar. But if I create the UIBarButtonItem using the "initWithTitle:" or "initWithBarButtonSystemItem:" method, the button show up. For example, look at the code below:
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil];
[self setToolbarItems:array];
If this is done, buttons show up on the toolbar. But, if I were to do the following:
UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
replyBtn.frame = CGRectMake(10, 0, 40, 40);
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil];
[self setToolbarItems:array];
In this code, only the item1 and item2 are displayed on the toolbar. replyButton is not shown on the toolbar. There is blank space at the place where the button is suppose to be at.
If this same code used on a regular toolbar that I create instead of NavigationController's toolbar, the button shows up. I am trying to just use one toolbar throughout the app to have the same feel that Apple's Mail application does. The reason that I need to use the "initWithCustomView:" method is because one of the icons is colored and this is the only way it shows up colored on a normal toolbar. Now, I have looked through apple documentation and there isn't any mention of why the "initWithCustomView:" method couldn't be called (or maybe I couldn't find it).
Could please somebody shine some light on this topic to help me point in the right direction. thanks in advance guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看不出与你尝试的有什么不同,但它最终对我有用,使用该代码:
注意与你的代码的一些差异(也许这就是问题所在?我不确定):
1.我的按钮不是在方法中本地分配的,而是在类中分配的(你知道,属性,合成等)
2.你的
我的看起来有点不同
尝试这些微小的改变,也许它会起作用:)
I can't see the difference from what you tried, but it eventually worked for me, with that code:
Note few differences from yours (maybe that's where the catch? I'm not sure):
1. my buttons are not allocated locally in the method, but in the class (you know, the property, synthesize, etc)
2. yours
where mine looks a bit different
Try these tiny changes, maybe it will work :)
我也遇到了同样的问题。事实证明,每次将新视图推入堆栈时,UINavigationController 的工具栏都会重置其项目。我试图在 applicationDidFinish 函数中设置工具栏项目,但它不起作用。一旦我在被推送到导航堆栈的 viewController 的 - (void) viewDidAppear 函数中设置工具栏 itms,它就起作用了。
因此,如果您希望导航控制器在整个应用程序中保留相同的工具栏项目,则必须在视图出现后推送到导航控制器的每个视图中设置工具栏项目。
我希望这有帮助!
I was having the same problem. It turns out that the toolbar for the UINavigationController resets it's items every time a new view gets pushed on the stack. I was trying to set the toolbar items in the applicationDidFinish function, and it was not working. It worked once I set the toolbar itms in the - (void) viewDidAppear function of the viewController that was being pushed onto the navigation stack.
So, it seems like if you want the navigation controller to keep the same toolbar items throughout the application, you have to set the toolbar items in each view that you push onto the navigation controller after the view appears.
I hope that helps!
我敢打赌该项目不会显示,因为它的视图中没有任何内容。您确定您的项目中有一个名为
Reply_Message.png
的图片吗?[UIImage imageNamed:@"Reply_Message.png"]
可能为nil
。I'd wager a guess that the item isn't showing up because its view doesn't have anything in it. Are you sure there's an image called
Reply_Message.png
in your project?[UIImage imageNamed:@"Reply_Message.png"]
might benil
.您可以在整个应用程序中使用一个工具栏,而无需使用导航控制器工具栏。由于您的代码适用于非 navigationController 工具栏,因此这可能是完成您想要的任务的最简单方法。在您的应用程序委托中,尝试向窗口添加工具栏,如下所示。这样它就可以在整个应用程序中保持不变。确保您考虑的方案可以让您访问工具栏以添加/删除按钮,或从不同的视图控制器隐藏/显示它。
You can use one toolbar throughout your app without using the navigation controllers toolbar. Since your code works with a non-navigationController toolbar, this might be the easiest way to accomplish what you want. In your app delegate, try adding a toolbar to window, like this. This way it is persistent throughout the app. Make sure you consider a scheme that will let you access the toolbar to add/remove buttons, or hide/show it from different view controllers.