Jaw Ali
Jaw Ali

Reputation: 205

how to set Image for UIBarButton item in ipad

I have tab bar button in item named login I want to assign the custom image for this any idea how it is possible?

I am doing following way but app crashes.

Here is the code:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(loginPressed)];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage imageNamed:@"loginN.png"] f

Upvotes: 0

Views: 171

Answers (2)

Jitendra
Jitendra

Reputation: 5081

Try this code

UIImage* image = [UIImage imageNamed:@"facebook.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frame];
    [someButton setBackgroundImage:image forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(facebookshare)
        forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem * rightbtn=[[UIBarButtonItem alloc]initWithCustomView:someButton];
    self.navigationItem.rightBarButtonItem=rightbtn;

this might be helpful to you...

Upvotes: 0

rptwsthi
rptwsthi

Reputation: 10182

Try it in this way:

UIImage *image = [UIImage imageNamed:@"facebook.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(loginPressed) forControlEvents:UIControlEventTouchDragInside];
UIBarButtonItem *rbBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rbBtn;

Upvotes: 1

Related Questions