replace background images with background color in all screen - background-color

I've facing one problem while developing my application.In my application there are more then 10 screens and have done with design too.But now i want to change whole color of the screen while user can open color picker and selected color will apply on all the screes.
Can any one suggest me that how can i do this one.because i have used lot's of imageview and images too.
Below is the example code where i have used the images in uitableview cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CategoriesListCell";
CategoriesListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CategoriesListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
[cell.contentView setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
int row = indexPath.row * 2;
NSLog(#"row:::%d",row);
UIButton *btnCategoriesLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btnWidth, rowHeight)];
UIImage *imgButton = [UIImage imageNamed:#"bgbutton"];
[btnCategoriesLeft setImage:imgButton forState:UIControlStateNormal];
[btnCategoriesLeft addTarget:self action:#selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];
[btnCategoriesLeft setTag:row];
//Set Transpernt images
UIImageView *imgViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 88, 53)];
[imgViewLeft setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",row]]];
[imgViewLeft setBackgroundColor:[UIColor clearColor]];
[btnCategoriesLeft addSubview:imgViewLeft];
//Set Title of label
UILabel *lblCategoriesTitleLeft = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 140, 20)];
lblCategoriesTitleLeft.backgroundColor = [UIColor clearColor];
lblCategoriesTitleLeft.textColor = [UIColor whiteColor];
[lblCategoriesTitleLeft setFont:FONT_WITH_SIZE_REGULAR(15.0)];
[lblCategoriesTitleLeft setText:[arrListOfCategories objectAtIndex:row]];
[lblCategoriesTitleLeft setTextAlignment:NSTextAlignmentCenter];
[btnCategoriesLeft addSubview:lblCategoriesTitleLeft];
[[cell contentView] addSubview:btnCategoriesLeft];
UIButton *btnCategoriesRight = [[UIButton alloc] initWithFrame:CGRectMake(btnWidth, 0, btnWidth, rowHeight)];
imgButton = [imgButton stretchableImageWithLeftCapWidth:imgButton.size.width/2 topCapHeight:imgButton.size.height/2];
[btnCategoriesRight setImage:imgButton forState:UIControlStateNormal];
[btnCategoriesRight setTag:row +1];
[btnCategoriesRight addTarget:self action:#selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];
//Set Transpernt images
UIImageView *imgViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(40, 30, 88, 53)];
[imgViewRight setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",row+1]]];
[imgViewRight setBackgroundColor:[UIColor clearColor]];
[btnCategoriesRight addSubview:imgViewRight];
//Set Title of label
UILabel *lblCategoriesTitleRight = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 140, 20)];
lblCategoriesTitleRight.backgroundColor = [UIColor clearColor];
lblCategoriesTitleRight.textColor = [UIColor whiteColor];
[lblCategoriesTitleRight setFont:FONT_WITH_SIZE_REGULAR(15.0)];
[lblCategoriesTitleRight setText:[arrListOfCategories objectAtIndex:row+1]];
[lblCategoriesTitleRight setTextAlignment:NSTextAlignmentCenter];
[btnCategoriesRight addSubview:lblCategoriesTitleRight];
[[cell contentView] addSubview:btnCategoriesRight];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
Thanks.

I am a bit confused about how your sample code correlates to the question you're asking. If I understand this correctly, you would like to change the background color of all 10 or more screens with a slider. Try setting up red, blue, and green sliders and assigning global variables via the app delegate.
Add three sliders to interface builder.
Then set up three UISlider outlets.
YourSliderViewController.h
#interface YourViewController ()
#property (nonatomic, weak) IBOutlet UISlider *redSlider;
#property (nonatomic, weak) IBOutlet UISlider *blueSlider;
#property (nonatomic, weak) IBOutlet UISlider *greenSlider;
#end
YourSliderViewController.m
- (IBAction)changeColor:(id)sender{
float red = self.redSlider.value;
float green = self.greenSlider.value;
float blue = self.blueSlider.value;
UIColor *newColor = [UIColor colorWithRed:red
green:green
blue:blue
alpha:1.0];
self.view.backgroundColor = newColor;
}
The following is to make your RGB sliders accessible to all classes. That way you can set self.view.background = newColor in any other ViewController inside -viewDidLoad.
Create a pointer to the AppDelegate. I called mine appDel.
MyXcodeProject-Prefix.pch
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
AppDelegate *appDel;
#endif
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIColor *newColor;
#end
AppDelegate.m
appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];

Related

Custom MKAnnotaionView, Display title/subtitle Position on Map

1) My annotation shows the title and subtitle of not.
I have already tried a lot, but I can not.
2) If an annotation has been selected, the annotation will be centered in the middle of the window. So the map is moved. Any idea?
3) My Callout button does not work anymore, I've got the # selector (openAnything) but I want to use this function is triggered - (void) mapView: (MKMapView *) mapView annotationView: (MKAnnotationView *) view calloutAccessoryControlTapped: (UIControl *) control {
Enough to ask, here's a video and some source code
http://www.youtube.com/watch?v=Ur1aqeYEFHw&feature=youtube_gdata_player
Thanks
TAPPMapViewControler.m
- (MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[TAPPMapAnnotation class]])
{
static NSString *shopAnnotationIdentifier = #"Filiale";
//pinView = (MKPinAnnotationView *)
TAPPMapAnnotationCustom* pinView = (TAPPMapAnnotationCustom*)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
[self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
if (pinView == nil)
{
// if an existing pin view was not available, create one
TAPPMapAnnotationCustom *customPinView = [[TAPPMapAnnotationCustom alloc] initWithAnnotation:annotation reuseIdentifier:shopAnnotationIdentifier];
customPinView.image = [UIImage imageNamed:#"map_pin.png"];
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
TAPPMapAnnotation *theAnnotation = view.annotation;
TAPPMapAnnotationDetail *theController = [self.storyboard instantiateViewControllerWithIdentifier:#"MapAnnotationDetail"];
theController.theAnnotationId = theAnnotation.objectId;
[self.navigationController pushViewController:theController animated:YES];
}
TAPPMapAnnotationCustom.h
#import <MapKit/MapKit.h>
#interface TAPPMapAnnotationCustom : MKAnnotationView
#property (strong, nonatomic) UIImageView *calloutView;
#property (strong, nonatomic) UILabel *title;
#property (strong, nonatomic) UILabel *subTitle;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- (void)animateCalloutAppearance:(BOOL)inAdd;
#end
TAPPMapAnnotationCustom.m
#import "TAPPMapAnnotationCustom.h"
#import "TAPPMapAnnotation.h"
#implementation TAPPMapAnnotationCustom
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
[super setSelected:selected animated:animated];
if(selected)
{
// Remove Image, because we set a second large one.
self.image = Nil;
UIImage *imageBack = [[UIImage imageNamed:#"map_pin.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 35, 0, 6)];
self.calloutView = [[UIImageView alloc]initWithImage:imageBack];
[self.calloutView setFrame:CGRectMake(0,0,0,0)];
[self.calloutView sizeToFit];
[self addSubview:self.calloutView ];
// Callout Button
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(openAnything) forControlEvents:UIControlEventTouchUpInside];
rightButton.frame = CGRectMake(0
, ((self.calloutView.frame.size.height-6) / 2)-(rightButton.frame.size.height / 2)
, rightButton.frame.size.width
, rightButton.frame.size.height);
self.rightCalloutAccessoryView = rightButton;
self.rightCalloutAccessoryView.hidden = YES;
self.rightCalloutAccessoryView.alpha = 0;
[self addSubview:self.rightCalloutAccessoryView];
// Start Annimation
[self animateCalloutAppearance:YES];
} else {
//Start Annimation and Remove from view
[self animateCalloutAppearance:NO];
}
}
- (void)didAddSubview:(UIView *)subview{
if ([[[subview class] description] isEqualToString:#"UICalloutView"]) {
for (UIView *subsubView in subview.subviews) {
if ([subsubView class] == [UIImageView class]) {
UIImageView *imageView = ((UIImageView *)subsubView);
[imageView removeFromSuperview];
}else if ([subsubView class] == [UILabel class]) {
UILabel *labelView = ((UILabel *)subsubView);
[labelView removeFromSuperview];
}
}
}
}
- (void)animateCalloutAppearance:(BOOL)inAdd {
if (inAdd == YES) {
self.rightCalloutAccessoryView.hidden = NO;
[UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
animations:^{
self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
, self.calloutView.frame.origin.y
, self.calloutView.frame.size.width+150
, self.calloutView.frame.size.height);
self.rightCalloutAccessoryView.alpha = 1;
self.rightCalloutAccessoryView.frame = CGRectMake(self.calloutView.frame.size.width - (self.rightCalloutAccessoryView.frame.size.width)
, self.rightCalloutAccessoryView.frame.origin.y
, self.rightCalloutAccessoryView.frame.size.width
, self.rightCalloutAccessoryView.frame.size.height);
} completion:^(BOOL finished){ }];
} else {
[UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
animations:^{
self.rightCalloutAccessoryView.alpha = 0;
self.rightCalloutAccessoryView.frame = CGRectMake(0
, self.rightCalloutAccessoryView.frame.origin.y
, self.rightCalloutAccessoryView.frame.size.width
, self.rightCalloutAccessoryView.frame.size.height);
self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
, self.calloutView.frame.origin.y
, self.calloutView.frame.size.width-150
, self.calloutView.frame.size.height);
} completion:^(BOOL finished){
self.image = [UIImage imageNamed:#"map_pin.png"];
[self.calloutView removeFromSuperview];
[self.rightCalloutAccessoryView removeFromSuperview];
}];
}
}
#end
Your annotation class needs to have a subtitle attribute, yours has subTitle.
If calloutAccessoryControlTapped is not being called it is usually a sign your MKMapview's delegate has not been set. However you're only setting the accessory button when it gets selected. Do you have a good reason for that? Since the callout window won't be visible until it is selected, I would advise you to set the accessory button in viewForAnnotation and let it appear when the callout window does.
I fix the first one:
TAPPMapAnnotation *theAnnotation = (TAPPMapAnnotation *)self.annotation;
self.title = [[UILabel alloc]init];
self.title.font = [UIFont systemFontOfSize:12.0];
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
self.title.text = theAnnotation.title;
[...]
[self.theView addSubview:self.title];

UITextView text does not get cleared

I have a small UIView that I hide/show in to show a message to the user. The message itself is in a UITextView that I add to the small UIView that gets shown.
The sliding-in and sliding-out works fine - but the prior messages are not cleared. I have spent enough time to fix the problem - but to no avail. Can someone lend me their eyes!!
Here is how the UITextField is created programatically:
#interface MessageVC : UIViewController {
UITextView *messageTV;
}
#property (nonatomic, retain) UITextView *messageTV;
- (id)init;
- (void)showMsg:(NSString *)title;
#end
and
- (id)init {
if (self = [super init]) {
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 100)] autorelease];
[self.view setBackgroundColor:[UIColor blackColor]];
}
return self;
}
- (void)showMsg:(NSString *)title {
[self setMessageTV : [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 315, 90 )]];
[[self messageTV] setBackgroundColor : [UIColor greenColor]];
[[self messageTV] setTextColor : [UIColor whiteColor]];
[[self messageTV] setText:#""]; <<<<<<<< - does not clear the text
[[self messageTV] setText : title];
[self.view addSubview : [self messageTV]];
[self.view setHidden:NO];
}
- (void) hideMessage {
[self.view setHidden:YES]
}
I'll go out on a limb and ask why you're using a UITextView. I honestly have never needed to use a UITextView. Try changing it to a UILabel and see if the problem is specific to the UITextView. If you absolutely need a UITextView, let me know in a comment, but I have a suspicion that a UILabel is what you're after.
It appears that you are adding it as a subview every time. So you're actually creating multiple UITextViews and adding them on top of each other. You would either need to removeFromSuperview or just set the text of the instance variable.
Take these two lines out of showMsg and put them in viewDidLoad:
[self setMessageTV : [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 315, 90 )]];
[self.view addSubview : [self messageTV]];

How to clear previous view when loading new detail view when a row is clicked in UITableView - iOS4

I need to display a detailed view controller with multiple labels when a row in a table view is clicked. The labels I am displaying are of dynamic height based on the content, which is being read from the iPhone's internal calendar. I have been able to implement the loading of detail views based on which row in the table view has been selected. My issue is that the previous detail view does not get cleared up when a new row is clicked. Especially in the case when my previous detail view had longer labels and the new detail view has shorter labels, I can see the content of the previous labels below the new one. How can I clear up the entire Detail View controller before reloading it with the new row's data. Following is the code I am using:
RootViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (mdController == nil) {
MtgDetailController *aController = [[MtgDetailController alloc] initWithNibName:#"MtgDetailController" bundle:nil];
self.mdController = aController;
[aController release];
}
self.mdController.mtgIndex = indexPath.row;
// mdController.mtgIndex = indexPath.row;
// [mdController setMtgIndex:indexPath.row];
// NSInteger temp = indexPath.row;
UIActionSheet *action = [[UIActionSheet alloc]
initWithTitle:#"Select an Option"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Open Meeting", #"Dial into meeting", nil];
[action showInView:self.parentViewController.view];
[action release];
}
MtgDetailController.m (This is the Detail View Controller)
-(void) viewDidAppear:(BOOL)animated {
// [self reloadInputViews];
Smart_MeetingAppDelegate *myDelegate = (Smart_MeetingAppDelegate *)[[UIApplication sharedApplication]delegate];
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
NSString *title = [myDelegate.titles objectAtIndex:self.mtgIndex];
NSString *mtg_time = [myDelegate.mtg_times objectAtIndex:self.mtgIndex];
NSString *loc = #"Conf Room 123";
// detail = #"test mtg test mtg. Dial number: 3334445555, (333)444-5555, 333-333-5555. ID: 4455333344, Password: 6576567";
detail = [myDelegate.detail_array objectAtIndex:self.mtgIndex];
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:#"\\b[0-9]+(\\-)?(\\))?(\\.)?(\\s)?([0-9]+)?(\\-)?(\\.)?(\\s)?([0-9]+)?(\\-)?(\\.)?(\\s)?([0-9]+)?\\b" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* matches = [regex matchesInString:detail options:0 range:NSMakeRange(0, [detail length])];
[regex release];
unichar chr[1] = {'\n'};
NSString *singleCR = [NSString stringWithCharacters:(const unichar *)chr length:1];
NSString *titleRow = [NSString stringWithFormat:#"%# %# %#", title, singleCR, mtg_time];
NSMutableAttributedString *attrTitle = [NSMutableAttributedString attributedStringWithString:titleRow];
[attrTitle setFont:[UIFont systemFontOfSize:18] range:[titleRow rangeOfString:title]];
[attrTitle setFont:[UIFont systemFontOfSize:16] range:[titleRow rangeOfString:mtg_time]];
NSMutableAttributedString *attrLoc = [NSMutableAttributedString attributedStringWithString:loc];
NSMutableAttributedString *attrDetail = [NSMutableAttributedString attributedStringWithString:detail];
[attrDetail setFont:[UIFont systemFontOfSize:16]];
CGSize suggestedSize = [attrTitle sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame1 = CGRectMake(10, 15, 300, suggestedSize.height);
OHAttributedLabel *lblTitle = [[OHAttributedLabel alloc] initWithFrame:frame1];
lblTitle.numberOfLines = 0;
CGSize size2 = [attrLoc sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame2 = CGRectMake(10, suggestedSize.height+15+5, 300, size2.height);
OHAttributedLabel *lblLocation = [[OHAttributedLabel alloc] initWithFrame:frame2];
CGFloat temp = size2.height+suggestedSize.height;
CGSize size3 = [attrDetail sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame3 = CGRectMake(10, temp+15+5, 300, size3.height);
OHAttributedLabel *lblDescription = [[OHAttributedLabel alloc] initWithFrame:frame3];
lblDescription.numberOfLines = 0;
lblTitle.attributedText = attrTitle;
lblLocation.attributedText = attrLoc;
lblDescription.attributedText = attrDetail;
for (NSTextCheckingResult *m in matches) {
NSString *num = [detail substringWithRange:m.range];
NSRange linkRange = [detail rangeOfString:num];
[lblDescription addCustomLink:[NSURL URLWithString:#"user://certa"] inRange:linkRange];
}
lblDescription.delegate = self;
[self.view addSubview:lblTitle];
[self.view addSubview:lblDescription];
[self.view addSubview:lblLocation];
[lblTitle release];
[lblLocation release];
[lblDescription release];
}
You add three new instances of OHAttributedLabel to your detail view every time viewDidAppear: executes. You never remove these views. You need to save references to them (lblTitle, lblDescription, and lblLocation) in your MtgDetailController, and either remove them or reuse them the next time viewDidAppear: executes.
Add three properties to your MtgDetailController:
#property (retain) UILabel *lblTitle;
#property (retain) UILabel *lblDescription;
#property (retain) UILabel *lblLocation;
(Don't forget to #synthesize them too.)
At the top of viewDidAppear:, remove the labels from their superviews:
- (void)viewDidAppear:(BOOL)animated {
[self.lblTitle removeFromSuperview];
[self.lblDescription removeFromSuperview];
[self.lblLocation removeFromSuperview];
...
At the bottom of viewDidAppear:, save your newly-created labels in the properties:
self.lblTitle = lblTitle;
self.lblLocation = lblLocation;
self.lblDescription = lblDescription;

navigationItem not showing on popoverController

The navigationbar is failing to appear, works fine in a UITableView, but fails inside a popoverController
Initiate a popover popoverController in UIViewController
-(IBAction) btnShowMovies:(id) sender {
if (self.popoverController == nil) {
teamAController *movies =
[[teamAController alloc]
initWithNibName:#"teamAController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
self.popoverController = popover;
[popover release];
}
CGRect popoverRect = [self.view convertRect:[btn frame]
fromView:[btn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
teamAController.h
#interface teamAController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSArray *theArray;
}
#property (nonatomic, retain) NSArray *theArray;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
-(void) createArray;
teamAController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= #"FooBarExtreme";
self.contentSizeForViewInPopover = CGSizeMake(250.0, 300.0);
[self createArray];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
Everything works, I have lovely table with images etc, correct sized and placed popover just no title bar..... ?
I found the solution/problem by following the tutorial at http://mobiforge.com/designing/story/using-popoverview-ipad-app-development.
Worth noting that I found this the most comprehensive one on creating uiPopoverController with uiNavigationBar elements from UIButtons.
The issue is that the popover itself belongs to the view that calls it. The content is derived from the xlib/view you load into it. But not the titlebar. You call that in the parent view view.
This code is in the main view and is called from the UIButton
// BookMarksViewController is the class that contains the code/xib for the popover's content
// Of overarching importance is creating it as a UITableViewController
if (self.popoverController == nil) {
BookMarksViewController *bookMarksViewController =
[[BookMarksViewController alloc]
initWithNibName:#"BookMarksViewController"
bundle:[NSBundle mainBundle]];
// Here's the rub: because in effect this view is controlling the popover
// we have to assign nav bar stuff here. Sigh.
bookMarksViewController.navigationItem.title = #"Territories";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:bookMarksViewController];
bookMarksViewController.contentSizeForViewInPopover = CGSizeMake(320, 400);
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
popover.delegate = self;
[bookMarksViewController release];
[navController release];
self.popoverController = popover;
[popover release];
}
CGRect sourceRect = [self.view convertRect:[btn frame] fromView:[btn superview]];
[self.popoverController presentPopoverFromRect:sourceRect
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

why I have got such kind of BarButton one upon other?

Hi!I have used UIBarButton Image but i have got such kind of issue.i mean Image+button on backside.for that i have written code below.
UIBarButtonItem *btnMap = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"mapbutton.png"] style:UIBarButtonItemStylePlain target:self action:#selector(btnMapClicked:)];
self.navigationItem.rightBarButtonItem = btnMap;
[btnMap release];
Use initWithCustomView and set a UIButton as the customView instead:
UIButton *subBtn = [UIButton buttonWithType:UIButtonTypeCustom];
subBtn.frame = CGRectMake(0, 0, 50, 40); //adjust as needed
[subBtn setImage:[UIImage imageNamed:#"mapbutton.png"] forState:UIControlStateNormal];
[subBtn addTarget:self action:#selector(btnMapClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnMap = [[UIBarButtonItem alloc] initWithCustomView:subBtn];
self.navigationItem.rightBarButtonItem = btnMap;
[btnMap release];
Note that the sender in btnMapClicked: will now be a UIButton instead of a UIBarButtonItem.

Resources