uinavigationController 的工具栏与自定义项目

发布于 2024-09-11 08:22:05 字数 1843 浏览 6 评论 0原文

我正在尝试在我的应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

澜川若宁 2024-09-18 08:22:05

我看不出与你尝试的有什么不同,但它最终对我有用,使用该代码:

 //////////////////////////////////////////////////////////////////////////////////////////////
/////>>>> Adding buttons for browsing in the toolbar of the popover's navigation controller///
//////////////////////////////////////////////////////////////////////////////////////////////

//>>>>Create a goBack button    
goBack = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *goBackImage = [UIImage imageNamed:@"back1.png"];
[goBack setImage:goBackImage forState:UIControlStateNormal];
[goBack addTarget:self action:@selector(goBackClicked:) forControlEvents:UIControlEventTouchUpInside];
goBack.frame = CGRectMake(0,0,goBackImage.size.width,goBackImage.size.height);
//Create a Bar button to hold this button
UIBarButtonItem *goBackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:goBack] autorelease]; 

UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil] autorelease];
NSArray *arrayOfButtons = [[[NSArray alloc] initWithObjects:goBackBarButton, flex1, addButton, nil] autorelease];
[self setToolbarItems:arrayOfButtons];

注意与你的代码的一些差异(也许这就是问题所在?我不确定):
1.我的按钮不是在方法中本地分配的,而是在类中分配的(你知道,属性,合成等)
2.你的

[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];

我的看起来有点不同

[goBack setImage:goBackImage forState:UIControlStateNormal];

尝试这些微小的改变,也许它会起作用:)

I can't see the difference from what you tried, but it eventually worked for me, with that code:

 //////////////////////////////////////////////////////////////////////////////////////////////
/////>>>> Adding buttons for browsing in the toolbar of the popover's navigation controller///
//////////////////////////////////////////////////////////////////////////////////////////////

//>>>>Create a goBack button    
goBack = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *goBackImage = [UIImage imageNamed:@"back1.png"];
[goBack setImage:goBackImage forState:UIControlStateNormal];
[goBack addTarget:self action:@selector(goBackClicked:) forControlEvents:UIControlEventTouchUpInside];
goBack.frame = CGRectMake(0,0,goBackImage.size.width,goBackImage.size.height);
//Create a Bar button to hold this button
UIBarButtonItem *goBackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:goBack] autorelease]; 

UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil] autorelease];
NSArray *arrayOfButtons = [[[NSArray alloc] initWithObjects:goBackBarButton, flex1, addButton, nil] autorelease];
[self setToolbarItems:arrayOfButtons];

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

[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];

where mine looks a bit different

[goBack setImage:goBackImage forState:UIControlStateNormal];

Try these tiny changes, maybe it will work :)

命硬 2024-09-18 08:22:05

我也遇到了同样的问题。事实证明,每次将新视图推入堆栈时,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!

我偏爱纯白色 2024-09-18 08:22:05

我敢打赌该项目不会显示,因为它的视图中没有任何内容。您确定您的项目中有一个名为 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 be nil.

昇り龍 2024-09-18 08:22:05

您可以在整个应用程序中使用一个工具栏,而无需使用导航控制器工具栏。由于您的代码适用于非 navigationController 工具栏,因此这可能是完成您想要的任务的最简单方法。在您的应用程序委托中,尝试向窗口添加工具栏,如下所示。这样它就可以在整个应用程序中保持不变。确保您考虑的方案可以让您访问工具栏以添加/删除按钮,或从不同的视图控制器隐藏/显示它。

    - (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    /////// frame sizes ////////
    // compensate for the status bar
    int statusBarHeight = 20;

    // toolbar
    int toolbarWidth   = window.frame.size.width;
    int toolbarHeight  = 30;  // I think standard size is 30
    int toolbarxOffset = 0;
    int toolbaryOffset = window.frame.size.height - tHeight;
    CGRect toolbarFrame = CGRectMake(toolbarxOffset,
                                     toolbaryOffset,
                                     toolbarWidth,
                                     toolbarHeight);

    /////// toolbar //////////////////////
    self.myPersistentToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
    [window addSubview:self.myPersistentToolbar];
}

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.

    - (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    /////// frame sizes ////////
    // compensate for the status bar
    int statusBarHeight = 20;

    // toolbar
    int toolbarWidth   = window.frame.size.width;
    int toolbarHeight  = 30;  // I think standard size is 30
    int toolbarxOffset = 0;
    int toolbaryOffset = window.frame.size.height - tHeight;
    CGRect toolbarFrame = CGRectMake(toolbarxOffset,
                                     toolbaryOffset,
                                     toolbarWidth,
                                     toolbarHeight);

    /////// toolbar //////////////////////
    self.myPersistentToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
    [window addSubview:self.myPersistentToolbar];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文