views:

165

answers:

1

Ive got an iPhone/iPad universal application and I wanted to have a custom navigation bar where the top half of the nav bar contained our companies logos, and the bottom half was the standard navigation bar.

I figured out how to do this, showing in the code below, but my UIButton "logosButton" doesnt always respond to being clicked, it appears as though only certain parts of the button are active, and others dont do anything at all... I cannot figure out why this is.

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] addSubview: navController.view];

    float width = IS_IPAD ? 768.0f : 320.0f;
    float logosHeight = IS_IPAD ? 20.0f : 20.0f;
    float barHeight = IS_IPAD ? 32.0f : 32.0f;

    self.navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, logosHeight, width, barHeight)];

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, logosHeight)];
    UIButton *logosButton = [[UIButton alloc] init];
    [logosButton setBackgroundImage:[UIImage imageNamed:@"logo_bar_alone.png"] forState:UIControlStateNormal];
    [logosButton setBackgroundImage:[UIImage imageNamed:@"logo_bar_alone.png"] forState:UIControlStateHighlighted];

    [logosButton addTarget:self action:@selector(logosButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [logosButton setFrame: CGRectMake(0, 0, width, logosHeight)];
    [tempView addSubview: logosButton];
    [[self view] addSubview: tempView];
    [tempView release];

    [[self view] addSubview: self.navBar];

    self.navItem = [[UINavigationItem alloc] initWithTitle: @"Home"];
    [self.navBar pushNavigationItem:self.navItem animated:NO];
}

The method: logosButtonClicked does get fired every now and then when I click on the UIButton, but I clearly am clicking on certain spots where nothing happens at all...

Very frustrating, I dont seem, to see a pattern in regards to where its active, but could someone please help out here?

EDIT

I think I have just stumbled across something, I changed the button to a UIButtonTypeRoundedRect and got rid of the images (and also made it larger) and it appears that I cannot click on the bottom OR top sections of the button where the button is rounded. So the middle rectangular section is the only section where I can click... why would this be?

EDIT 2 For anyone reviewing this question, please see taber's latest edit on his answer. The Navigation Controller is eating touches for some reason, and I need to figure out why this is so, could be a bug?

+1  A: 

FINAL ANSWER :)

Okay after reviewing the project it looks like the UINavigationController that is positioned below the button is eating touches globally. Which is definitely weird because it was added as a subview BELOW your button. So there must be some kind of funky frame/touch-eating going on with the UINavigationController. I tried to set navigationController.titleView.userInteractionEnabled = NO but to no avail. If anyone knows how to basically make UINavigationController and UINavigationBar not eat touches (I'm talking about the background where no buttons exist) then please chime in. The UIButton touches work just fine when the UINavigationController isn't added to the view.

 

Original Answer

It may be some other UI elements in [self view] or tempView sitting on top of the button and intercepting taps. You might want to try either commenting out everything else in the view(s) and/or try setting the userInteractionEnabled property on them like this:

[otherUIElement setUserInteractionEnabled: NO];

EDIT: This code makes it so that you CAN click the round rect edges but NOT the title text. So I suspect that it's some sort of subview preventing the button from catching the tap.


[logosButton addTarget:self action:@selector(logosButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[logosButton setFrame: CGRectMake(0, 0, 320.0, 50.0)];
[logosButton setTitle: @"HELLO" forState: UIControlStateNormal];
[logosButton.titleLabel setUserInteractionEnabled: YES];

EDIT #2:

Try using this subclassed button and see if the issue still occurs:


// NavBtn.h

@interface NavBtn : UIButton {

}

-(id)initWithTitle:(NSString *)text;

@end

// NavBtn.m

#import "NavBtn.h"

@implementation NavBtn

- (id)initWithTitle:(NSString *)text {
  if ((self = [super initWithFrame: CGRectMake(0, 0, 320, 50)])) {
    [self setTitle: text forState: UIControlStateNormal];
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    [self setBackgroundImage:[UIImage imageNamed:@"logo_bar_alone.png"] forState: UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:@"logo_bar_alone.png"] forState: UIControlStateHighlighted];
  }
  return self;
}

// i guess you shouldn't need to mess with layoutSubviews 

@end

To load it into your view:


#import "NavBtn.h"

... in viewDidLoad, etc ...
NavBtn *btn = [[NavBtn alloc] initWithTitle: @"hey"];
[self.view addSubview: btn];
[btn release];

If THAT doesn't work, there's got to be some kind of third party library doing some funky category overriding of your UIButton class or something.

taber
I think I have just stumbled across something, I changed the button to a UIButtonTypeRoundedRect and got rid of the images (and also made it larger) and it appears that I cannot click on the bottom OR top sections of the button where the button is rounded. So the middle rectangular section is the only section where I can click... why would this be?
Mark
that's really weird! i'm not sure, but as a workaround you might want to try going about creating the button a different way... like subclassing UIButton and then setting your background images in the layoutSubviews method of your subclassed button.
taber
thanks for the edit, but still no luck, Im completely out of ideas...
Mark
update, this is actually happening to other buttons in my app!!!!! All I am doing is setting buttons background images, like so: [myButton setBackgroundImage:[UIImage imageNamed:@"btn_date.png"] forState:UIControlStateNormal]; [myButton setBackgroundImage:[UIImage imageNamed:@"btn_date_off.png"] forState:UIControlStateHighlighted]; I can send out a project with this behaviour to you if you like?
Mark
does is still happen when you create buttons using Interface Builder?
taber
yep, it does, I have tried to replicate using a testing project, but no luck yet
Mark
what version of the SDK are you using? also, does it happen both in the simulator as well as on a device?
taber
at the moment, 4.1, but it was happening on 4.0 and 3.2
Mark
Thanks so much for the help, that didnt work but I DEFINATELY think something massively funky or stupid on my behalf happening here...
Mark
damn, must be. maybe try building on a different computer or re-installing the sdk from scratch. :( good luck!
taber
thanks, quick question, is the mouse accurate on the simulator? is the point of the mouse cursor the actual hit location?
Mark
i'm pretty sure it's the center of the touch, yes
taber
I have managed to create a testing application that mimics the behaviour if you would like to take a look?
Mark
sent! thanks for helping out with this
Mark