当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Title on custom UINavigationItem is not shown

问题描述:

I'm creating custom UINavigationItem with custom background image and title. The image is shown but the title doesn't. What could be wrong?

I'm doing it this way:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal];

[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected];

[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted];

[button setTitle:@"Cancel" forState:UIControlStateNormal];

[button setTitle:@"Cancel" forState:UIControlStateSelected];

[button setTitle:@"Cancel" forState:UIControlStateHighlighted];

[button addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];

[button setFrame:CGRectMake(0, 0, 49, 30)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

网友答案:

You need to set image as backgroundImage not direct. If you set image like this

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

the title will not be displayed

set like this

[button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
网友答案:

I think you must remove:

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

After that, the title will appear

网友答案:

Either remove this line

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

or you can change the Title color to some other color.i hope it will solve your problem.

make it to DrakgrayColor or some other color which can overcome your image color.

网友答案:

OK. Found a workaround, however I'm not sure whether it is allowed:

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close",@"Close") style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonClicked)];
[self.navigationItem setLeftBarButtonItem:closeButton];

for (UIView *view in self.navigationController.navigationBar.subviews) {        
    if ([[[view class] description] isEqualToString:@"UINavigationButton"]) {
        UINavigationButton *button = {(UINavigationButton *)view};
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted];

    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: