关于uibutton:带有自定义项目的uinavigationController的工具栏

uinavigationController's toolbar with custom items

我正在尝试在我的应用中使用NavigationController的工具栏。假定此工具栏的toolbarItems根据显示的视图控制器而改变。这是非常基本的。

我想做的是使用UIBarButtonItem的" initWithCustomView:"方法向工具栏添加自定义按钮。但是,该按钮将不会显示在工具栏上。但是,如果我使用" initWithTitle:"或" initWithBarButtonSystemItem:"方法创建UIBarButtonItem,则会显示该按钮。例如,查看下面的代码:

1
2
3
4
5
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];

完成后,按钮将显示在工具栏上。但是,如果我要执行以下操作:

1
2
3
4
5
6
7
8
9
10
11
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。 ReplyButton未显示在工具栏上。假定按钮所在的位置有空白。

如果在我创建的常规工具栏(而不是NavigationController的工具栏)上使用了相同的代码,则会显示该按钮。我试图在整个应用程序中仅使用一个工具栏,以具有与Apple Mail应用程序相同的感觉。我需要使用" initWithCustomView:"方法的原因是,其中一个图标是彩色的,这是它在普通工具栏上显示彩色的唯一方法。现在,我浏览了Apple文档,没有提到为什么无法调用" initWithCustomView:"方法(或者也许找不到)。

请有人对此主题提供一些启发,以帮助我指出正确的方向。在此先感谢大家。


我遇到了同样的问题。事实证明,每次将新视图推入堆栈时,UINavigationController的工具栏都会重置其项目。我试图在applicationDidFinish函数中设置工具栏项,但它不起作用。一旦我在被推到导航堆栈上的viewController的-(void)viewDidAppear函数中设置了工具栏itms,它就会起作用。

因此,如果您希望导航控制器在整个应用程序中保留相同的工具栏项目,则必须在视图出现后将其推入导航控制器的每个视图中设置工具栏项目。

我希望能对您有所帮助!


我看不到与您尝试的区别,但最终对我有用,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //////////////////////////////////////////////////////////////////////////////////////////////
/////>>>> 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.您的

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

我的看上去有些不同

1
[goBack setImage:goBackImage forState:UIControlStateNormal];

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


您可以在整个应用程序中使用一个工具栏,而无需使用导航控制器工具栏。由于您的代码可与非navigationController工具栏一起使用,因此这可能是完成所需内容的最简单方法。在您的应用程序委托中,尝试将工具栏添加到窗口中,如下所示。这样,它在整个应用程序中都是持久的。确保考虑使用一种方案,该方案将允许您访问工具栏以添加/删除按钮,或从其他视图控制器隐藏/显示它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    - (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];
}


我猜测该项目未显示是因为它的视图中没有任何内容。您确定项目中有一个名为Reply_Message.png的图像吗? [UIImage imageNamed:@"Reply_Message.png"]可能是nil