UIToolbar custom background image - objective-c

in loadView method I set
self.navigationController.toolbarHidden = NO;
That will display a toolbar at the bottom of every view.
Now I want to have a custom background for every toolbar. So in app delegate I have following code:
#implementation UIToolbar(CustomImage)
-(void)drawRect:(CGRect)rect{
UIImage *image = [UIImage imageNamed:#"toolbar.png"];
[image drawInRect:self.bounds];
}
#end
but this has no effect. The color has still this standard blue color. How can I have a custom background?

Related

UITabBar unselected icon tint

I am trying to change the color of my tab bar icons when the tabs are UNselected. Right now the color is default grey and I can change the color to whatever color I want for when it IS selected.
Apple's dev library said to change the image rendering to "original" instead of its default mode "template." I did that. then it says to use initWithTitle:image:selectedImage: I tried to do that as well but I think that's where I messed up. I wrote this in my viewcontroller.m file. What's wrong here?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
stat = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
(instancetype)initWithTitle:(NSString *)nil image:(UIImage *)stat selectedImage:(UIImage *)stat;
}
The problem is that you are using the same UIImage with UIImageRenderingModeAlwaysOriginal in both places.
Your code should look something like
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
UIImage *statAlwaysOriginal = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:statAlwaysOriginal selectedImage:stat];
The other thing is that there are some actual syntax errors in your post (in the UITabBarItem initialization, but I suspect you just pasted it incorrectly.

Visible buttons with transparent navigation bar

I've seen several apps that have completely transparent navigation bars but with visible buttons, I cant seem to find anything that wont make the button invisible as well. I'm sure they were using UINavigationController for the navbar because it had the same animations with fade and what not.
Im currently using this code in ViewDidLoad and ViewDidAppear to either hide or show the nav bar because it's not supposed to be on the first page-
[self.navigationController setNavigationBarHidden:NO animated:YES];
and this code for its transparency:
[self.navigationController.navigationBar setAlpha:0.0];
Create a subclass of UINavigationBar containing no methods except drawRect:. Put custom drawing code there if you need to, otherwise leave it empty (but implement it).
Next, set the UINavigationController's navigation bar to this subclass. Use initWithNavigationBarClass:toolBarClass: in code, or just change it in the Interface Builder if you're using storyboards/nibs (it's a subclass of your UINavigationController in the hierarchy at the side).
Finally, get a reference to your navigation bar so we can configure it using self.navigationController.navigationBar in the loadView of the contained view controller. Set the navigation bar's translucent to YES and backgroundColor to [UIColor clearColor]. Example below.
//CustomNavigationBar.h
#import <UIKit/UIKit.h>
#interface CustomNavigationBar : UINavigationBar
#end
//CustomNavigationBar.m
#import "CustomNavigationBar.h"
#implementation CustomNavigationBar
- (void)drawRect:(CGRect)rect {}
#end
//Put this in the implementation of the view controller displayed by the navigation controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = YES;
[self navigationController].navigationBar.backgroundColor = [UIColor clearColor];
}
Here's a screen shot of the result, mimicking Plague.
The blue border was drawn in drawRect: to show you that a UINavigationBar is there and not just a button and a label. I implemented sizeThatFits: in the subclass to make the bar taller. Both the button and label are UIView's containing the correct UI element that were placed in the bar as UIBarButtonItems. I embedded them in views first so that I could change their vertical alignment (otherwise they "stuck" to the bottom when I implemented sizeThatFits:).
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
To make the Navigation Bar transparent, use the below code:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
After this set the background image of the Navigation Bar the same as the view behind it using the below property:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"SAMPLE.jpg"] forBarMetrics:UIBarMetricsDefault];

Navigation controller affecting app overall background image

I have an application where when it begins, it loads a viewController which loads the Default.png launch image and holds it there for 1.5 seconds, then that image fades out, then fades into another image which will be the background image for my entire application. From here it loads the first viewController and then the buttons and navigation bar fade in.
So it goes SplashScreeVC - Default.png presented
Default.png Fades out
New Image fades in (This is the apps overall background image)
Loads MainVC with buttons and nav bar alpha set to 0.1.
Method called to fade in the nav bar and the buttons in the mainVC
So in my app Delegate I have this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
SplashVC *splash = [[SplashVC alloc] initWithNibName:#"SplashView" bundle:nil];
self.window.rootViewController = splash;
[self.window makeKeyAndVisible];
return YES;
}
So the app loads with the Default.png, which then loads the splashVC which has the Default.png as its background image, this is shown for 1.5 seconds. I then have a some methods to fade out this image, and then fade in the image that will be used as the background image for the entire application. Then it calls the appDelegates MainNav method which loads the Navigation controller. The app Delegates MainNav method is below
-(void) MainNav
{
UIViewController *main = [[MainVC alloc] initWithNibName:#"MainView" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:main];
UIImage *navImage = [UIImage imageNamed:#"NavBarGrey.png"];
//Add the custom image
[[self.navController navigationBar] setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
//[[UINavigationBar appearance] setTintColor:UIColorFromRGB(0xee2e24)];
[[UINavigationBar appearance] setTintColor:UIColorFromRGB(0xe7e7e7)];
self.navController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"backgroundImage.png"]];
self.window.rootViewController = self.navController;
}
The problem is when it moves from the SplashScreen VC to the MainVC, the image suddenly jerks up despite the fact it is the same image that fades in in the SplashVC.
Does anyone have any idea why this is? I am guessing it has something to do with the navigation controller affecting the image size, but not sure what to do to fix it? Do I need to create a new image for the navigation bar background with different dimensions? Or is there another way I can fix this issue?
Any advice would be much appreciated. Thanks!!
Edit: Fixed image jerking to the right, had image width at wrong height, but the image is still jumping up a set amount
Managed to figure it out. Seems to have to make a copy of the image and increase its height by 20 pixels and set that as the navigation bars background image. This offsets the navigation bar problem.
you can set bacround image code like
yourView.setBacroundColur=[UIColour colourWithImagePttern:[UIImage ImageNamed:#""]];

Custom drawing on top of UIButton's background image

What is the method calling sequence of UIButton's drawing?
I have a UIButton subclass, let's call it UIMyButton. I add it to a .xib controller and give it a background image. Then in the UIMyButton.m file I override drawRect method and draw a shape.
Something like:
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
CGContextFillRect(c, rect);
}
The problem is that it draws the orange rectangle underneath the background image. What methods are responsible for drawing/updating background images (it updates on "tap down" for example)? How do I force the draw on top without adding an additional custom subview to the button (or is the background image a subview itself)?
I'm just trying to get a clear understanding of how the UIButton drawing sequence works.
The solution is to not use a background for the button. Instead, draw the image inside the drawRect method like so:
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx=UIGraphicsGetCurrentContext();
UIImage *image = [UIImage imageNamed:#"image.jpg"];
[image drawInRect:self.bounds];
CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
CGContextFillRect(c, rect);
}

change tabbar icon color from default blue

i m trying to change the tabbar icon color from default blue to red...i m getting this error
"Stray '\342'in program".. i m getting the error at "-(void)recolorItemsWithColor:......." and also at the implementation section...is der anyway to solve dis error...is there anyother method to change the tab bar icon from default blue to some other color
#interface UITabBar (ColorExtensions)
– (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
#end
In the class where you define the tab bar set the property of the
tabBarItem to ->>
UITabBarItem *tabBarItem1 = [[self.tabBar.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"campaigns_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"campaigns.png"]];
Its a property of tabBarItem and u can change the default blue image to a custom image.
campaigns_hover.png is the selected custom image AND
campaigns.png is the custom image when not selected...
Enjoy the secret.. :)
It does not uses a private API.. The function is defined under UITabBarItem.h class.
go to your asset folder, find the asset and click on Identity Inspector , and change "Render As" to Original Image (assuming your icon is the color you want it)
Try adding a 49x49 png image into your project, then paste these line of code into your app delegate inside the applicationDidFinishLaunching and before adding a subview.
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *view = [[UIView alloc] initWithFrame:frame];
UIImage *tabBarBackgroundImage = [UIImage imageNamed:#"49x49.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:tabBarBackgroundImage];
[view setBackgroundColor:color];
[color release];
[[tabcontroller tabBar] insertSubview:view atIndex:0];
[view release];
Hope it will help.
Are you aware that the code you're trying to use uses private APIs and thus will cause your apps to be rejected?
I don't know about the specific error you're seeing. But if you're looking for another solution, one that'll make it into the App Store, you could try PESTabBarAdditions.