Friday, May 31, 2013

UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics example in Objective C (iOS).


UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics


Sets the back button vertical position offset for given bar metrics.

- (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics

Parameters
adjustment
The back button vertical position offset for barMetrics.
barMetrics
Bar metrics.

Discussion of [UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics]
This modifier applies only to navigation bar back buttons and is ignored by other buttons.

This offset is used to adjust the vertical centering of bordered bar buttons within the bar.

UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics example.
[myBackButton setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsDefault];
[myBackButton setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsLandscapePhone];

Example of [UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics].
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];

    [[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] setBackgroundVerticalPositionAdjustment:3 forBarMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] setBackButtonBackgroundVerticalPositionAdjustment:3 forBarMetrics:UIBarMetricsDefault];

UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics example.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Navigation bar

    UIImage *navigationBackground = [[UIImage imageNamed:@"EMSNavigationBarBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
    [[UINavigationBar appearance] setBackgroundImage:navigationBackground forBarMetrics:UIBarMetricsDefault];

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-3.f forBarMetrics:UIBarMetricsDefault];

    //Normal button

    UIImage *button30 = [[UIImage imageNamed:@"EMSBarButtonItemNormal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    UIImage *buttonHighlighted30 = [[UIImage imageNamed:@"EMSBarButtonItemPressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    [[UIBarButtonItem appearance] setBackgroundImage:button30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundImage:buttonHighlighted30 forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-3.f forBarMetrics:UIBarMetricsDefault];

    //Back button

    UIImage *buttonBack30 = [[UIImage imageNamed:@"EMSBarButtonItemBackNormal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
    UIImage *buttonBackHighlighted30 = [[UIImage imageNamed:@"EMSBarButtonItemBackPressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBackHighlighted30 forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:-3.f forBarMetrics:UIBarMetricsDefault];

    return YES;
}

End of UIBarButtonItem setBackButtonBackgroundVerticalPositionAdjustment forBarMetrics example article.