UIAppearence Support for IOS 6 - unexpected results - objective-c

Below code is perfectly working fine iOS 5, but not on iOS 6 or above.
What I want that for Email composer sheet the navigationBar image will be different then other UINavigationBar classes. I can't understand that debug pointer is responding the appearance method but on device it shows navigationBar image as "bgNavigationBar.png"; expected is "bgNavigationBar_2.png".
Please guide me.......
if ([[UINavigationBar class]respondsToSelector:#selector(appearance)]) {
UIImage *logoImage44 = [[UIImage imageNamed:#"bgNavigationBar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:logoImage44 forBarMetrics:UIBarMetricsDefault];
UIImage *ImagePlain = [[UIImage imageNamed:#"bgNavigationBar_2.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage:ImagePlain forBarMetrics:UIBarMetricsDefault];
}

This thing is not working in ios6.
[[UINavigationBar appearanceWhenContainedIn:[MFMessageComposeViewController class], nil] setBackgroundImage:[UIImage imageNamed:#"bgNavigationBar_2.png"] forBarMetrics:UIBarMetricsDefault];
Just you need to set this property in your Mail Handler Class.
if (![[UINavigationBar class]respondsToSelector:#selector(appearance)])
{
UIView *backgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,44)]autorelease];
[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bgNavigationBar_2.png"]]];
controller.topViewController.navigationItem.titleView = backgroundView ;
}
else
{
UIImage *gradientImagePlain = [[UIImage imageNamed:#"bgNavigationBar_2.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImagePlain forBarMetrics:UIBarMetricsDefault];
}
and then reset another image for all other navigation controller's background image.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self.parentController dismissModalViewControllerAnimated:YES];
UIImage *gradientImagePlain = [[UIImage imageNamed:#"bgNavigationBar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImagePlain forBarMetrics:UIBarMetricsDefault];
}
Hope this will work for you.

Related

Subclass UINavigationbar for customise in iOS7 does not work

I have made a subclass of the navigation bar.
In there I set a background image and I would like to disable the clipping, because my Image has some shadow and an arrow which is higher than the navigation bar.
in iOS 6&5 it works perfectly when I remove the tick Clip Subviews in the Interface Builder in Xcode.
In iOS7 unfortunately the background image is always clipped...
I also tried to add the following lines:
- (void) awakeFromNib
{
// Initialization code
UIImage *image = [UIImage imageNamed:#"tt_navigation_bar.png"];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) image = [UIImage imageNamed:#"tt_navigation_bar-ios7.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue-Bold" size:21.0], UITextAttributeFont, [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:36.0/255.0 alpha:1], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
for (UIView *v in [self subviews]) {
NSLog(#"v: %#", v);
v.layer.masksToBounds = NO;
}
self.layer.masksToBounds = NO;
}
I solved this by manually editing my two images into the top n pixels and then "the rest" and setting the image of "the rest" to the shadowImage.

change background image of navigation bar based on orientation

Is there a way to change the background image of navigation bar based on orientation? I tried using methods in the app delegate such as :
-(void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == (UIInterfaceOrientationMaskPortrait)||orientation==(UIInterfaceOrientationMaskPortraitUpsideDown)){
UIImage *myImage = [UIImage imageNamed:#"borderP.png"];
UIImage * imageBottom = [UIImage imageNamed:#"bottomP.png"];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
NSLog(#"******P******");
}
else if(orientation == (UIInterfaceOrientationMaskLandscapeLeft||orientation== UIInterfaceOrientationMaskLandscapeRight)){
UIImage *myImage = [UIImage imageNamed:#"borderL.png"];
UIImage * imageBottom = [UIImage imageNamed:#"borderL.png"];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
NSLog(#"******L********");
}
}
but the back ground image does not change.
Any advice?
The appearance proxy only applies only to new instances. To modify an instance that already exists, you you need to call setBackgroundImage:forToolbarPosition:barMetrics: on it directly.

How to remove navigation bar's shadow

I have UINavigationBar.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 46.0f, 320.0f, 50.0f)];
navBar.tintColor = [UIColor blackColor];
[self.view addSubview:navBar];
And I want to remove standart top shadow. How can I do this?
You can define a custom image for the navbar in iOS 5.0 and above:
UIImage *backgroundImage = [UIImage imageNamed:#"bevel_nav_bar.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

Move UIBarButtonItem few pixels down?

I have created a custom UINavigationBar which is a tiny bit taller than Apples default navigation bar.
I can’t seem to find a way to move the UIBarButtonItem down to be directly centered between the two dashed lines.
Is there an easy way to do this? I’ve tried creating a custom button but had no success. Ideally I would just like to move the default back button down a couple of pixels.
Code used to create UINavigationBar, custom header image and UIBarButtonItem:
//Create image for navigation background
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:#"navbackground.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground
forBarMetrics:UIBarMetricsDefault];
//Centered title image
UIImageView *headerImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"hometitle.png"]];
[headerImage setFrame: CGRectMake(0, 0, 180, 49)];
self.navigationItem.titleView = headerImage;
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
[backBarButtonItem setTintColor: [UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]];
[backBarButtonItem setStyle: UIBarButtonItemStylePlain];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
Thanks in advance,
Create the UIBarButtonItem using a custom view. This custom view will be a UIView with the actual UIButton (as a subview) placed x pixels from the top (x=the number of pixels you want to move it down).
sorry
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton1 setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal]; myButton1.showsTouchWhenHighlighted = YES;
myButton1.frame = CGRectMake(0.0, 3.0, 50,30);
[myButton1 addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
self.navigationItem.leftBarButtonItem = leftButton;

UINavigationItem leftBarButton is hidden behind it's background

I have a UINavigationBar with a custom background and my own back button. I did the following to try achieve this.
//custom background
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navigation-bar.png"]] autorelease];
[self.navigationController.navigationBar insertSubview:background atIndex:0];
//custom back button
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
The back button always works, but it can only be seen if I don't apply my custom background. Could someone please point out how to achieve both?
Thanks in advance.
Ricky.
You can set background image of navigation bar by using this
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if([self isMemberOfClass:[UINavigationBar class]])
{
UIImage *image;
image=[UIImage imageNamed:#"nav_bar_img"];
CGContextClip(ctx);
CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx,
CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
else
{
[super drawLayer:layer inContext:ctx];
}
}
#end
I ended up doing the following.
#implementation UINavigationBar (Category)
- (void)drawRect:(CGRect)rect
{
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.navImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Every time I want to change the background of the bar, I change the delegate's navImage property and call -setNeedsDisplay on my navigation bar.
Thanks for your help.