Thursday, 15 March 2012

iPhone Development:Add custom image in navigation bar button item


As per the tittle of  post, I will be going to show you some steps that allows you to add image to your
barbuttonItem in iPhoen SDK.
Below are the steps that allow us to add image to our barbuttonitem









Step 1: Create an instance of image, the one you want to display in your bar button.
UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
Step 2: Create a rect/frame according to your image height and width. 
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
Step 3: Create an instance of your UIButton and initialize it with frame 
created in step 2. 
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
Step 4: Set image created in step 1 as your button background image. 
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
Step 5: Add target to your button.  
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
Step 6: Create your barButton Item and init it with customview.In your
customview pass the button instance created in step 3.UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] 
initWithCustomView:someButton];
Step 7: Assiign barbutton created in step 5 to your navigation
controller rightbarbuttonitem 
self.navigationItem.rightBarButtonItem=mailbutton;
Step 8: Release the button. 
[someButton release];

No comments:
Write comments