Quote:
Originally Posted by fiftysixty
I struggled with this problem too once, and the solution I found was to make a UISegmentedControl with the custom image you want, and then setting the properties of the segmented control and creating a UIBarButtonItem with the segmented control as the custom view. The problem is still that it doesn't look like UIBarButtonItem with a string title, the background color is not the same, but it's better than using UIButton I think. Anyway, here is the code:
Code:
UIImage *customIcon = [UIImage imageNamed:@"yourImagepng"];
UISegmentedControl *customButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:customIcon, nil]];
customButton.segmentedControlStyle = UISegmentedControlStyleBar;
customButton.momentary = YES;
[customButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventAllEvents];
customBarButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
For some reason you have to use UIControlEventAllEvents for the segmented control, using UIControlEventTouchUpInside doesn't work.
|
UIImage *customIcon = [UIImage imageNamed:imgName];
UISegmentedControl *customButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:customIcon, nil]];
customButton.segmentedControlStyle = UISegmentedControlStyleBar;
customButton.momentary = YES;
[customButton addTarget:self action:@selector(buttonHint

forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.rightBarButtonItem = customBarButton;
//change the button frame
customBarButton.customView.frame = CGRectMake(0,0,40,0);
[customButton release];
[customBarButton release];