Sunday, February 03, 2013

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem's custom view.

This is an example on how you can achieve that. It takes a target, selector and image name and returns a UIBarButtonItem as an image.

+(UIBarButtonItem*) getBarButton:(id)target withSelector:(SEL)selector andImage:(NSString*)imageName
{
    //Create image
    UIImage *image = [UIImage imageNamed:imageName];
    
    //Create UIButton with the imag
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    //Create UIBarButtonItem with customview button
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    return barButton;
}

6 comments:

tom said...

nice

essaykings.co.uk said...

This proved out to be extremely useful in terms of home assignment at the university! I like the idea of your blog very much!

kevin antony said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.


rpa Training in annanagar

blue prism Training in annanagar

automation anywhere Training in annanagar

iot Training in annanagar

rpa Training in marathahalli

blue prism Training in marathahalli

automation anywhere Training in marathahalli

blue prism training in jayanagar

automation anywhere training in jayanagar

Pankaj Singh said...

Thank you much for this tutorial; this is informative and valuable blog. Visit for
Europe Honeymoon Packages

Veav by nimisha priya said...

Very nice post.
buy tops for girls

https://amantoothtown.blogspot.com/ said...

Great..thanks for sharing this information..
dentist in vasant kunj

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...