Duck
Duck

Reputation: 35993

iPhone . UIToolbar not working with some buttons

I have a UIToolbar.

If I create a UIBarButton like this, it works:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:iOB style:UIBarButtonItemStylePlain target:self action:@selector(myStuff)];
[button setTitle:@"Hi"];

If I create the UIBarButton like this, cause I want it to be colored:

UIImageView *vUP = [[UIImageView alloc] initWithImage:iUP];
UIBarButtonItem *UPButton = [[UIBarButtonItem alloc] initWithCustomView:vUP];
[UPButton setAction:@selector(anotherStuff)];
[UPButton setTitle:@"Hi";
[UPButton setTarget:self];
[vUP release];

The button shows, WITHOUT title and will doesn't respond to any touch...

am I missing something?

Upvotes: 1

Views: 1139

Answers (2)

Sudesh Kumar
Sudesh Kumar

Reputation: 569

UIImage* image3 = [UIImage imageNamed:@"plain_btn-75-30.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);

UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];   
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];
    [someButton setTitle:@"Back" forState:UIControlStateNormal];
    someButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];

    [someButton setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];

     UPButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];

If you want image and title both then image must be set as a background image with title.

Upvotes: 1

Till
Till

Reputation: 27597

Use a UIButton instead of the UIImageView as a UIImageView does not respond to touch events. You will, under no circumstance get a title as you provided a custom view.

Upvotes: 1

Related Questions