Showing posts with label setBackgroundImage. Show all posts
Showing posts with label setBackgroundImage. Show all posts

Tuesday, June 11, 2013

UIToolbar setBackgroundImage example in Objective C (iOS).

UIToolbar setBackgroundImage

Sets the image to use for the background in a given position and with given metrics.

- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics

Parameters of [UIToolbar setBackgroundImage]
backgroundImage
The image to use for the toolbar background in the position specified by topOrBottom and with the metrics specified by barMetrics.
topOrBottom
A toolbar position constant.
barMetrics
A bar metrics constant.

UIToolbar setBackgroundImage example.
if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
        //iOS 5 new UINavigationBar custom background
        [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
}
To realize this, take a look at here iOS 4.3 to iOS 5.0 API Differences and search for "UINavigationBar.h"


Example of [UIToolbar setBackgroundImage].
//toolBar background image set based on iOS version
    [[UIDevice currentDevice] systemVersion];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

        //iOS 5
        UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"]; 

        if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
            [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0];
        }

    } else {

        //iOS 4
        [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0];

    }

UIToolbar setBackgroundImage example.
// Set the background image for PortraitMode
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"Sample.png"]
                                   forBarMetrics:UIBarMetricsDefault];
// and For LandscapeMode
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"Sample.png"]
                                   forBarMetrics:UIBarMetricsLandscapePhone];

End of UIToolbar setBackgroundImage example article.

Monday, June 10, 2013

UITabBar setBackgroundImage example in Objective C (iOS).


UITabBar setBackgroundImage

The background image for the bar.

@property(nonatomic, retain) UIImage *backgroundImage

Discussion of [UITabBar setBackgroundImage]
A stretchable background image is stretched, a non-stretchable background image is tiled.

UITabBar setBackgroundImage example.
Like mentioned before on iOS 5 I would suggest you use the background image:

UITabBar *tabBar = tabController.tabBar;
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    tabBar.backgroundImage = [UIImage imageNamed:@"TabBackground.png"];
}

Example of [UITabBar setBackgroundImage].
// UITabBar+Custom.m

#import "UITabBar+Custom.h"
#import <QuartzCore/QuartzCore.h>

-(void)setTabBarBackground:(UIImage *)backgroundImage  {
     if([self respondsToSelector:@selector(setBackgroundImage:)]) {
          // ios 5+
          [self setBackgroundImage:backgroundImage];
     }  else  {
          // ios 3.x / 4.x
          self.layer.contents = (id)backgroundImage.CGImage;
     }
}

UITabBar setBackgroundImage example.
// not supported on iOS4   
UITabBar *tabBar = [tabController tabBar];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
{
    // set it just for this instance
    [tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_brn.jpg"]];

    // set for all
    // [[UITabBar appearance] setBackgroundImage: ...
}
else
{
    // ios 4 code here
}

End of UITabBar setBackgroundImage example article.

Sunday, June 9, 2013

UINavigationBar setBackgroundImage example in Objective C (iOS).


UINavigationBar setBackgroundImage

Sets the background image for given bar metrics.

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics

Parameters of [UINavigationBar setBackgroundImage]
backgroundImage
The background image to use for barMetrics.
barMetrics
A bar metrics constant.
UINavigationBar setBackgroundImage example.
To apply image to all your navigation bars, use the appearance proxy:

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
For an individual bar:

// Assuming "self" is a view controller pushed on to a UINavigationController stack
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

Example of [UINavigationBar setBackgroundImage].
if( [self.navigationController.navigationBar respondsToSelector: @selector(setBackgroundImage:forBarMetrics:)]) {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"]
                                                  forBarMetrics:UIBarMetricsDefault];

}else {
    UIImageView* imgBG = [[UIImageView alloc]init];
    imgBG.image = [UIImage imageNamed:@"navbar.png"];
    imgBG.frame = CGRectMake(-10,0, 330, 44);
    [parentViewLeft addSubview:imgBG];

}
UIBarButtonItem *customBarButtomLeft = [[UIBarButtonItem alloc] initWithCustomView:parentViewLeft];
self.navigationItem.leftBarButtonItem = customBarButtomLeft;

UINavigationBar setBackgroundImage example.
If you use iOS4, you can override - (void)drawRect

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
    UIImage *navBg = [UIImage imageNamed:@"navBar.png"];
    [navBg drawInRect:rect];
}
@end
iOS 5,

if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

        UIImage *navBarImg = [UIImage imageNamed:@"navBar.png"];

        [self.navigationController.navigationBar setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];

    }
}

End of UINavigationBar setBackgroundImage example article.

Friday, May 31, 2013

UIBarButtonItem setBackgroundImage example in Objective C (iOS).


UIBarButtonItem setBackgroundImage


Sets the background image for a given state and bar metrics.

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

Parameters
backgroundImage
The background image for the specified state and metrics.
state
A control state.
barMetrics
Bar metrics.

Discussion of [UIBarButtonItem setBackgroundImage]
For good results, backgroundImage must be a stretchable image.

UIBarButtonItem setBackgroundImage example.
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"I am done" style:UIBarButtonItemStyleBordered target:self action:nil];
UIImage *stretchable = [[UIImage imageNamed:@"StretchableImage.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:16];
[btnDone setBackgroundImage:stretchable forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setRightBarButtonItem:btnDone];

Example of [UIBarButtonItem setBackgroundImage].
- (void) __setStyle:(UIBarButtonItemStyle)style {
    [self __setStyle:style];

    if(style == UIBarButtonItemStyleDone) {
        [self setBackgroundImage:[[UIImage imageNamed:@"customDoneButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:[UIImage imageNamed:@"customDoneButtonClicked"]
                        forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    } else {
        [self setBackgroundImage:[[UIImage imageNamed:@"customButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
                        forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [self setBackgroundImage:[UIImage imageNamed:@"customButtonHighlighted"]
                        forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    }
}

UIBarButtonItem setBackgroundImage example.
+ (UIBarButtonItem *)createSquareBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // Since the buttons can be any width we use a thin image with a stretchable center point
    UIImage *buttonImage = [[UIImage imageNamed:@"SquareButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"SquareButton_pressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    [[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
    [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button setTitle:t forState:UIControlStateNormal];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];
}

End of UIBarButtonItem setBackgroundImage example article.

Thursday, May 30, 2013

UIButton setBackgroundImage example in Objective C (iOS).

UIButton setBackgroundImage

Sets the background image to use for the specified button state.

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

Parameters
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.

Discussion of [UIButton setBackgroundImage]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setBackgroundImage example.
UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

btnClear.frame = CGRectMake(115, 200, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

Example of [UIButton setBackgroundImage].
+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
    {
   UIButton *button = [UIButton alloc] initWithFrame:frame;

   button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

   [button setTitle:title forState:UIControlStateNormal];
   [button setTitleColor:UIColor blackColor forState:UIControlStateNormal];

   UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newImage forState:UIControlStateNormal];

   UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

   [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

   // in case the parent view draws with a custom color or gradient, use a transparent color
   button.backgroundColor = UIColor clearColor;

   return button;
}

UIButton setBackgroundImage example.
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {

    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
    customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
    customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    customButton.titleEdgeInsets = edgeInsets;
    UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];

    CGSize size = CGSizeMake(30.0f, 30.0f);
    if (title != nil) {
        size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
    }
    customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
    customButton.layer.shouldRasterize = YES;
    customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}

End of UIButton setBackgroundImage example article.

Thursday, April 18, 2013

UIButton setBackgroundImage example objc


setBackgroundImage:forState:
Sets the background image to use for the specified button state.
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
Parameters
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state. (setBackgroundImage:forState:)

Example for (  )
{
    NSString            *title = @"My button title";
    UIColor              *titleColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
    UIImage             *bgImage = [UIImage imageNamed:@"yourImage.png"];

    UIImage             *highlightedBgImage = [UIImage imageNamed:@"yourImage.png"];
    UIButton            *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // Create button.
    //
    button.bounds = CGRectMake( 0.0f, 0.0f, bgImage.size.width, bgImage.size.height );
    button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.titleLabel.minimumFontSize = 10.0f;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightedBgImage forState:UIControlStateHighlighted];

    // You must specify your target object and action selector here.
    // 
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

}