开发者

Custom UINavigationBar button not displaying

(edited: added more information at bottom)

Firstly I have looked through similar posts and tried their answers but with no luck :( I have a custom navigation bar (just a custom background image) and I'm trying to add a custom back button.

// custom back button.
UIButt开发者_高级运维on *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backImg = [UIImage imageNamed:@"btn_back.png"];
backBtn.titleLabel.text = @"";
[backBtn setBackgroundImage:backImg forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 33);

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem.title = @"";

The above code is meant to add the button but it does not display on the nav bar. I can still click the area where its meant to appear and it does bring me back to the previous view?! removing the above code displays the standard back button.

Any ideas? Thanks in advance!

(edit)

Hi again guys,

I tried all your suggestions and still no luck :( I probably should have mentioned earlier that I have a custom nav bar image using the following code. This might be affecting the display of my custom back button image.

This code displays a custom image depending upon a parameter I set. Thanks again for further help!

// custom navigation bar image
@implementation UINavigationController (CustomImage)

// set the background image for the nav bar.
- (void) setCustomNavBar:(NSInteger)screen {

UIImage *navBarImg;
switch (screen) {
    case kCreateHuntScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-hunt.png"];
        break;

    case kCreateLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-location.png"];
        break;

    case kListNewLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_choose-location.png"];
        break;

    default:
        break;
}

UIImageView *imgView = [[UIImageView alloc] initWithImage:navBarImg];
[[[self.navigationBar subviews] objectAtIndex:0] removeFromSuperview];
[self.navigationBar addSubview:imgView];
[imgView release];
}

@end


Thanks again for all your advice! I finally found a solution :D

Very quickly I needed to be able to change the background image for a navigation bar from view to view when I pushed or popped the stack.

Using the most popular methods such as overriding the UINavigationBar's drawRect would only partly work, it would work on the root view but then push the image to the front of the navigation bar's stack for every other screen no matter if I sent it to the back, etc.

I found the solution here: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

Take a read, it works great :)


UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"imgname.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(method_name:) forControlEvents:UIControlEventTouchUpInside];
btn.frame=CGRectMake(3, 2, 53, 30);
UIBarButtonItem *btnBack=[[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem=btnBack;
[btnBack release];
[btn release];


Try,

self.navigationItem.hidesBackButton = YES;

befor writing the code for .leftBarButtonItem .Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜