Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem with custom view not showing after first view

OK this is a weird one. I've been slamming my head against it for an hour now trying to debug.

I've created a UIBarButtonItem with a custom view, which is a UIButton with an image assigned to it. The code is as follows

UIButton *btnAdd = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 28)] autorelease];

[btnAdd setImage:[UIImage imageNamed:@"btn_add"] forState:UIControlStateNormal];

[btnAdd addTarget:self
           action:@selector(addBox)
 forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithCustomView:btnAdd];

self.navigationItem.rightBarButtonItem = add;

It is located in my - (id)initWithNibName: method on an extended UIViewController

On the FIRST appearance of the UIViewController, the button appears as normal, and works fine. HOWEVER, if I push to another view controller, and then hit back, the button is INVISIBLE.

Even when it is invisible, the button still works. I can click on it, and it behaves normally. I know for a fact it is the original UIButton there, as I've done traces on its memory address and other properties... it's hidden property is FALSE, and its alpha property is set to 1.00f

I thought at first this was perhaps a depth issue, but I have code pushing my custom UINavigationBar background to the back on every frame loop, and nothing changes. SEE UPDATE

It's almost like the image is releasing or something behind the scenes, but the pointer on that is also fine...

I'm guessing this is some weird drawing quirk that I just need to monkey around with, but it's a very hard problem to google for.

Any hints would be appreciated.

UPDATE

It turns out this IS a depth issue. I am drawing a custom background on drawRect:, and that code DOES push the custom background to the back of the view stack, HOWEVER, this method is only being called once by the private framework methods... therefore when the bar is redrawn after the first view, the background isn't being forced back and for some stupid reason, it decides to redraw the buttons behind it.

So right now I am trying to figure out how I can force the background to be pushed back every time the UINavigationBar is rendered.

like image 636
M. Ryan Avatar asked Sep 01 '11 03:09

M. Ryan


1 Answers

try Like This its work Fine With Me

    UIImage *image = [UIImage imageNamed:@"btn_set-as-fea.png"];
    UIImage *hilghtedImage=[UIImage imageNamed:@"btn_set-as-fea_h.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);

    UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
    [someButton addTarget:self action:@selector(SetAsFeatured) forControlEvents:UIControlEventTouchUpInside];
    [someButton setBackgroundImage:image forState:UIControlStateNormal];
    [someButton setBackgroundImage:hilghtedImage forState:UIControlStateHighlighted];
    [someButton setShowsTouchWhenHighlighted:YES];
   setAsFeatured = [[UIBarButtonItem alloc] initWithCustomView:someButton];
like image 102
Srinivas Avatar answered Oct 08 '22 19:10

Srinivas