iOS Bar at top resembling UINavigationBar styleBlack - iphone

I'm not using a navigation controller, but I want to create a bar at the top of my UIViewController that resembles a UINavigationBar with the style of UIBarStyleBlack.
How can I do something like that?
Thanks!
iOS, xcode 4.3.3, ARC

you should following this:
typically navigationBar height is 44.0f;
- (void)viewDidLoad
{
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].statusBarFrame.size.width, "your_want_height")];
navBar.barStyle = UIBarStyleBlack;
[[self view] addSubview:navBar];
[super viewDidLoad];
}

Related

iOS spacing issue with translucent navigation bar

I'm currently having a UIViewcontroller with a 3-segment UISegmentedControl that when clicked switches view controllers displayed. The navigation bar and tab bar of this view are translucent.
I initialize the view like this:
- (void)viewDidLoad {
[super viewDidLoad];
[self setAutomaticallyAdjustsScrollViewInsets:YES];
self.segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"View 1",#"View 2",#"View 3",nil]];
[self.segControl setTintColor:[[ConstantsSingleton sharedConstants] default_bg_Color]];
[self.segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[self.segControl setSelectedSegmentIndex:0];
self.navigationItem.titleView = self.segControl;
//Setting up the first viewcontroller
UIViewController *vc = [self viewControllerForSegmentIndex:self.segControl.selectedSegmentIndex];
[self addChildViewController:vc];
vc.view.frame = self.contentView.bounds;
[self.contentView addSubview:vc.view];
self.currentViewController = vc;
}
The contentView is a IB defined UIView with 0 leading and trailing on all sides (so basically it fills the parent view).
I switch viewcontrollers the following way:
-(void)segmentChanged:(UISegmentedControl *)sender {
UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
[self addChildViewController:vc];
[self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
vc.view.frame = self.contentView.bounds;
[self.currentViewController.view removeFromSuperview];
[self.contentView addSubview:vc.view];
} completion:^(BOOL finished) {
[vc didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = vc;
}];
self.navigationItem.title = vc.title;
}
Now whenever I run this with a opaque navigation bar and tab bar this works fine, but whenever I try to use a translucent navigation bar and/or tab bar only the first view gets resized/its insets adjusted properly to not be behind the translucent navigation bar and/or tab bar. The second and third view will still appear behind them when they appear on screen.
It doesn't matter which viewcontroller is set as the first viewcontroller, all lead to the same behaviour.
What could be causing this issue and is there a way to solve this without resolving to manual contentinset adjustments.
I would suggest to enable the option Extend Edges in the property inspector of you're view.
Here's a good stackoverflow post differentiating the various layout settings for iOS 7 and up:
Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Transparent UITabBarController with changeable view in background

I want to have a UITabBarController, which is has an alpha of 0.5, whose transparency allows you to see a view in the background. The background view has to be accesible and changeable.
I'm able to add the background using this technique: https://gist.github.com/1157542
It's a Category which adds a subview to the UITabBarController, and sends the subview to the back. However, because it's a category, I can't make the subview a property. So I can't really access it.
Is there a way to make this background view more flexible and accessible? So I could, for instance, add other subviews to it easily from any of the tab bar controller's view controllers?
Instead of a category, you should subclass UITabBarController. This will allow you to have finer control over the object. Here's an example of a subclass.
// MPCustomTabBar.h
#interface MPCustomTabBar : UITabBarController
- (void) setBackgroundImage:(UIImage *)image;
#end
// MPCustomTabBar.m
#interface MPCustomTabBar
- (void) setBackgroundImage:(UIImage *)image {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.backgroundColor = [UIColor colorWithPatternImage:i];
[[self view] addSubview:imageView];
[[self view] sendSubviewToBack:imageView];
[[self view] setOpaque:NO];
[[self view] setBackgroundColor:[UIColor clearColor]];
[imageView release];
}
#end
Now you can do all the customization you want, alloc and init your new subclass by something like this:
MPCustomTabBar *bar = [[MPCustomTabBar alloc] init];
A solution to my problem might be simply this..
In my AppDelegate, just before [self.window makeKeyAndVisible];
self.theImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
[self.tabBarController.view insertSubview:self.theImage atIndex:0];
I can then easily change this image, in any of the tab bar controller's view controllers, like:
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.theImage.image = [UIImage imageNamed:#"image2.png"];
.. no categories required.
Here's what worked for me:
Make TabBar transparent (either by Storyboard or programmatically) by setting the Tint- and Background to Clear Color and Opaque to No.
The black background color is actually the window's colour. Assign the image you want to use as the background to the window itself:
UIImage *i = [UIImage imageNamed:#"main_background.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
[self.window setBackgroundColor:c];

DisMiss the Navigation bar image

I want to dismiss my navigation bar image(not the background image).That is i have set navigation bar image on the left side . when i go to the next controller the image overlaps the back button . This is my home screen with image in blue marked .
And this my second screen With image overlaped
And this the code i used to set the image in the bar
UINavigationBar *bar = [self.navigationController navigationBar];
UIImageView *barImg=[[UIImageView alloc]initWithFrame:CGRectMake(2, 3, 49, 39)];
barImg.image=[UIImage imageNamed:#"smalllogo.png"];
[bar addSubview:barImg];
[barImg release];
Now i dont want the image in my other screens what can i do for that?
One thing you can do is Add your image in side the viewWillAppear: method and remove it from the navigation bar when viewWillDisappear: method. Related code block as follows.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UINavigationBar *bar = [self.navigationController navigationBar];
UIImageView *barImg=[[UIImageView alloc]initWithFrame:CGRectMake(2, 3, 49, 39)];
barImg.image=[UIImage imageNamed:#"smalllogo.png"];
barImg.tag = 100;
[bar addSubview:barImg];
[barImg release];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
UINavigationBar *bar = [self.navigationController navigationBar];
[[bar viewWithTag:100] removeFromSuperview];
}
Or Here's another simple solution. Add below code segment to viewWillAppear: method
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"smalllogo.png"]]];
[self.navigationItem setLeftBarButtonItem:item];
[item release];
You have added the image in the navigation bar but not removed it before moving into other view.
Use `
-(void)viewWillDisappear:(BOOL)animated{
[barImg removeFromSuperview];
}`
to remove it from superview.

setTint not working

I have a been looking around to change the tint of my UINavigationBar on iOS 5.1.
I have a UITabBarController with two UINavigationControllers attached, the following code is placed in the custom UINavigationController:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
}
...but this does not have any effect.
Thanks in advance.
If you have subclassed UINavigationController (and not having a UIViewController inside a UINavigationController which is most common) then you should do this instead:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationBar setTintColor:[UIColor blackColor]];
}
because self is already a navigation controller. Otherwise your code you would have worked just fine (ie You had a UIViewController or one of its subclasses and you've embedded it via code or via Interface Builder into a UINavigationController)
try this
(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
}

Change the background color of UINavigationBar

I'd like to change the background color of an UINvaigationBar and I used tintColor property but it's not changing the color. You can checkout the code below.
In my Appdelegate I create a UITabBarController
tabbar = [[UITabBarController alloc] init];
Then I create a UINavigationController and attach my UITableViewController to it
UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:myViewController];
Then attached the UINavigationControllers to my tabbar like this
[tabbar setViewControllers:viewControllers]; //viewControllers is an array of UINavigationControllers
I tried setting the property tintColor in myAppdelegate like this
[[myNavigation navigationBar] setTintColor:[UIColor redColor]];
But this didn't work as expected and so I tried the same in my ViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.title = #"Test";
}
And still nothing happens. Please checkout the image below to see how navigation bar looks now.
Appreciate your help.
Cheers
Jugs
As a work around you may can use image on place of tint color please check below code for same.
if ([myNavigation respondsToSelector:#selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
[myNavigation setBackgroundImage:[UIImage imageNamed:BAR_IMAGE_NAME] forbarMetrics:UIBarMetricsDefault];
}else {
[myNavigation insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_IMAGE_NAME]] autorelease] atIndex:0];
}