Subclass UINavigationbar for customise in iOS7 does not work - objective-c

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.

Related

How to reduce font size of navigation bar title in iOS 7

My App has a view controller with a navigation controller on top. The navigation bar title is 'Forgot Password'. This title was shown without any line breaks in iOS 5 and 6. However when I run the same App in iOS 7, the navigation bar title is shown with dots at the end - 'Forgot Passw…' The only way to rectify this defect is to reduce the for size of the navigation bar title.
Could someone tell me how should I do is ? Is there any APIs in iOS 7 exclusively for this purpose. Is it possible to reduce the font size of navigation bar title for this view controller alone ? Any suggestions would be welcome. Thanks in advance.
UILabel *nav_titlelbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.navigationItem.titleView.frame.size.width,40)];
nav_titlelbl.text=#"Tests";
nav_titlelbl.textAlignment=NSTextAlignmentCenter;
UIFont *lblfont=[UIFont fontWithName:#"FontinSansCR-Bold" size:20];
[nav_titlelbl setFont:lblfont];
self.navigationItem.titleView=nav_titlelbl;
I used following code in my project and its work for me :
/* ios 7 Change */
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
//self.edgesForExtendedLayout = UIRectEdgeNone;
//self.automaticallyAdjustsScrollViewInsets = NO;
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x4B678B)];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"Helvetica Neue" size:21.0], NSFontAttributeName, nil]];
// self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
//[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
Below is the code that I am using in my own project(iOS 7)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set the title text property, color, font, size and so on
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor WhiteColor],NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"your font" size:21.0], NSFontAttributeName, nil]];
return YES;
}
Since iOS7, I always do like following:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:SIZE_YOU_LIKE],NSFontAttributeName,
nil]];
The Keyword UITextAttributeFont has been depressed from iOS7, you'd supposed to replace with
NSFontAttributeName.
Just set SIZE_YOU_LIKE a value you like~

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.

UINavigation bar button background image size

I'm trying sot set image background for the left back button navigation bar for all the app with this snippet:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0)
{
// iPhone 5.0 code here
UIImage *image = [UIImage imageNamed: #"btn_back.png"];
image = [image stretchableImageWithLeftCapWidth:40.0f topCapHeight:0.0f];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
return YES;
}
I've been trying different values in stretchableImageWithLeftCapWidth: but best result was this:
How can I set background image to the correct size?
Thanks
stretchableImageWithLeftCapWidth:topCapHeight: is deprecated. Use resizableImageWithCapInsets: instead.
This should work:
UIImage *buttonImage = [[UIImage imageNamed:#"btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 10, 1, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Could not set Image on Navigation bar only in "ios 5.0"

In my universal app,
I am setting an image on navigation bar...using **
objective c category...on UINavigationBar
**
The code works fine in ios 5.0 in iphone
*The code works fine in ios 4.3 in iPhone/iPad*
*But not working in **ios 5.0 iPad***
- (void) drawRect:(CGRect)rect
{
UIImage *image;
image = [UIImage imageNamed: #"Navigation.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self setTintColor:[UIColor clearColor]];
NSLog(#"Draw Rect");
}
To set a BG image for navigation bar in iOS 5 you can use the below code
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Navigation.png"] forBarMetrics:UIBarMetricsDefault];
}
But please keep in mind that this wont work in iOS 4. To make it work in both you also need to add a category to UINavigationBar
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:#"Navigation.png"];
[img drawInRect:rect];
}
If you want to set the background application-wide you can use iOS5's UIAppearance.
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsLandscape];

UINavigationBar setBackgroundImage: forBarMetrics: Not Working

I just switched over to iOS 5 and everything appears to be working in my application aside from the custom navigation bar. I looked around and followed everybody's suggestion of calling the new methods setBackgroundImage: forBarMetrics: however it doesn't appear to work. This is the code I've tried to place both within the app delegate and within the viewDidLoad method of some of the view controllers:
UINavigationBar *nb = [[UINavigationBar alloc]init];
if( [nb respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:#"navBarBackground.png"];
[nb setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
[nb release];
Unfortunately this doesn't work. If anybody has any suggestions at all, I'm all ears!
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];
In your example, the background image won't change because nb isn't hooked up to anything.
Answer by rob is correct but if application runs on iOS 4.3 or lower app will crash. So you can implement this like
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
}
This will set image for both mode Landscape and Portrait
You need to get the navigation bar from a navigation controller. Right now you are just creating one and then it deallocs when you release it. You need to get the navigation controller for your view controller.
UINavigationController * navigationController = [self navigationController];
UINavigationBar * nb = [navigationController navigationBar];
Note that if you want to apply the same to a toolbar, it is slightly different. Here's an example, building on Rob's answer:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBarDarkWoodGrain.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
You didn't do something like use a UINavigationBar subclass? If you do this and override drawRect: on iOS5 and above, things will break.
See my answer here for a way to support both OS versions.
This worked for me.
UIView *parentViewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
parentViewLeft.backgroundColor = [UIColor clearColor];
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;