I have a custom navigation bar showing a logo with an image and the status bar appears white. I'd like to be able to set the status bar color to match the greyish background of the logo.
http://i.imgur.com/wn0oOX5.png
When a cell is selected, I have the navigation bar going back to default and the status bar matches the navigation bar.
http://i.imgur.com/btQOvXn.png
LogoNavBar.m:
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width, 90);
return newSize;
}
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"logo.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
MasterViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[LogoNavBar alloc]init] forKeyPath:#"navigationBar"];
}
DetailViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[UINavigationBar alloc]init] forKeyPath:#"navigationBar"];
}
I've tried:
[[UINavigationBar appearance] setBarTintColor:[UIColor *colorhere*]];
in the app delegate and elsewhere but to no luck.
Related
Like the Instagram app, i would like to have my center tab bar item a specific color.
I understand this is how I would set the selected image:
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:0.484 green:0.403 blue:0.884 alpha:1] forSize:CGSizeMake(64, 49) withCornerRadius:0]];
But how would I keep the 3rd tab's background to always stay that color?
Also, how do I make the middle tab separate from the rest, as in, when I press the center tab, there is modal presentation of a view controller (like instagram), and the previous tab stays selected for when dismissing the modal view controller.
#interface BaseViewController : UITabBarController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage: (UIImage*)highlightImage;
#implementation BaseViewController
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init] ;
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
Subclass this Tab controller and call the above function to set image o tab bar items and center button like Instagram
#interface InstagramViewController : BaseViewController
#end
#implementation InstagramViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllers = [NSArray arrayWithObjects:
[self viewControllerWithTabTitle:#"Feed" image:[UIImage imageNamed:#"112-group.png"]],
[self viewControllerWithTabTitle:#"Popular" image:[UIImage imageNamed:#"29-heart.png"]],
[self viewControllerWithTabTitle:#"Share" image:nil],
[self viewControllerWithTabTitle:#"News" image:[UIImage imageNamed:#"news.png"]],
[self viewControllerWithTabTitle:#"#user" image:[UIImage imageNamed:#"123-id-card.png"]], nil];
}
-(void)willAppearIn:(UINavigationController *)navigationController
{
[self addCenterButtonWithImage:[UIImage imageNamed:#"cameraTabBarItem.png"] highlightImage:nil];
}
#end
When I set background image with method
setBackgroundImage:forBarMetrics:
it's rendered like this on iPhone 6
If i set the navigation bar to translucent, it's stretched normally.
#implementation OHCNavigationBar
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self setupGradient];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self setupGradient];
}
return self;
}
- (void)setupGradient {
UIImage *gradientImage = [UIImage imageNamed:#"navigationBarBackground.png"];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
#end
You can set UINavigationBar background image with non repeat mode by setting edge to 0.
UIImage *gradientImage32 = [[UIImage imageNamed:#"bkg_top_header_default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32
forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsDefault];
Update 1:
- (void)setupGradient {
UIImage *gradientImage = [[UIImage imageNamed:#"navigationBarBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
May this help you.
Enjoy Coding !!
I am looking to recreate the raised tab bar as seen in many apps but am looking to do this in a universal app where the raised position would need to change.
At the moment I am not having much luck creating this so have come here for enlightenment, failing this I may just create a custom tab bar background.
If this is the case, would this work in the app delegate to differentiate the two devices?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:#"iPadtabBar.png"]];
}
else {
UITarBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage: [UIImage imageNamed:#"iPhonetabBar.png"]];
}
You may be thinking, why doesn't he just test it.. well that would be easier and if I could I would rather than ask first but I don't get my Mac back until the start of next week!
You will have to subclass UITabBarController and customize that center tab button. There is a nicely done example here: https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar/Classes
Header file:
#interface CustomTabBarViewController : UITabBarController{
}
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage;
#end
Implementation file:
#implementation CustomTabBarViewController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init];
viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
I want my UINavigationBar to rotate properly within my SignatureViewController. This is currently how it looks in Portrait vs Landscapemode when rotating: http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg
As you can see the UINavigationBar does not scale on the width when in landscape. My project is built as following:
rootViewController(UINavgationController) ------Modal push-----> SignatureViewController
(portrait only) (portrait/landscape)
Note that I am not using a normal push to the SignatureViewController, I am using a modal one. Just in case I wanna mention this because I am not sure if it can affect the outcome in any strange way?... I have tried different approaches to change the background of the NavigationBar depending on if it is in landscape or portrait. I will post some here below:
try nr1: Not working
- (void) viewWillAppear:(BOOL)animated {
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr2: Not working
- (void) viewWillAppear:(BOOL)animated {
UIImage *navBarLandscape = [[UIImage imageNamed:#"navbar25g.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(x,x,x,x)];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:navBarLandscape forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr3: Not working
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
UIView *v = [[UIView alloc]init];
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a){
case 4:
v.frame = CGRectMake(0,20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:1");
break;
case 3:
v.frame = CGRectMake(-20,0,w-20,h+20);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:2");
[self.view addSubview:v];
break;
case 2:
v.frame = CGRectMake(0,-20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:3");
break;
case 1:
v.frame = CGRectMake(20,0,w-20,h+20);
NSLog(#"willrotate:4");
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
}
Note that in try nr3 the NSLog works but it refuses to change the background of the UINavigationBar. So, any ideas what I shall do from here ? / Regards
can you please try to implement the UINavigationBar class and override one drawRect: method
it should work
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *navimage;
//we will check if screen width is 320 then it will be potrait
if(self.frame.size.width == 320)
{
navimage = [UIImage imageNamed: #"Your Potrait nav bar image"];
}
else
{
navimage = [UIImage imageNamed: #"Your Landscape nav bar image"];
}
[navimage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Hope it helps...
and just try this for nr1 and nr2
just change the last line of code with this..if its working or not...let me know
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; //UIViewAutoresizingFlexibleWidth
i have 3 navigation controller and i want to change each background using different image. I was implement a category that extends UINavigationBar like this :
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];}
#end
but it make every navigation bar have a same background image. And then i try to implement code like this :
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
in every controller but each background just show the tintColor, not the image...what should I do???
and how if i want to do that too in tabbarController??
On each of your views, implement the following in your viewWillAppear: method
#import <QuartzCore/QuartzCore.h>
...
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"yourImageBgHere"].CGImage;
}
Cheers,
Rog