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];
Related
I have a ViewController with a Navigation Controller before it, that has a TableView and other elements inside. The tableView has also a Search Bar, and in the scene, besides the main ViewController I have a Search Display Controller.
The functionality is working, but I have the following UI issues:
Although my main view has black background colour(and most of the elements have black background colour), when I drag the tableView downwards there appears some white space.
I also have the following initial setup for UI
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
self.tableView.backgroundColor = [UIColor blackColor];
self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.searchDisplayController.searchResultsTableView.separatorColor = [UIColor blackColor];
self.searchDisplayController.searchResultsTableView.separatorInset = UIEdgeInsetsMake(0, 12, 0, 12);
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor blackColor]];
[self.searchDisplayController.searchContentsController.view setBackgroundColor:[UIColor blackColor]];
self.searchDisplayController.searchBar.barStyle = UIBarStyleBlack;
self.searchDisplayController.displaysSearchBarInNavigationBar = NO;
}
When I search for a result, and drag the table upwards after the Search Bar, there is a small place where the table view is visible.
Any ideas how can I fix this :(?
Edit update:
Both were solved removing the Search Bar from the TableView. After this another issue came that the animation of Search Display Controller wasn't updating the Search Bar. this was solved with the following post:
UISearchBar animation issue
You have to set UIView background color to black (not just UITableView color).
You have to position your table view below UISearchBar, you can do this either programmatically or via IB. I recommend second option. Pretty easy.
I am building iOS8 app. On my tableview controller, I am using self.navigationController.hidesBarsOnSwipe = YES, to hide the navigationBar on swipe up gesture. It is working nicely, but my statusBar becomes transparent and shows the table content underneath.
On storyboard, Status Bar are Top Bar are set to "Inferred"
I want to:
1. Keep my status bar opaque
2. Maintain the same color as the navigationBar
3. Table content scrolls underneath the statusBar
Thank you.
Here is a Swift solution:
First, change UITableViewController to UIViewController and add a tableView field.
Then, implement your viewDidLoad method as follows:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.frame
view.addSubview(tableView)
let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
topBar.backgroundColor = myDesiredColor
view.addSubview(topBar)
}
You can add a constraint to the top layout, by this scrolling content will not appear below the status bar.
Make a custom View.
UIView * statusBarView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:statusBarView];
Im trying to change the icon tint of my unselected tab bar icon images. I have used the patch code below, however, the post that i found this patch code in says to run this in the tab bar controller but i did not know how to do that so i ran it in the -(void)viewDidLoad method in the viewcontroller .m file. It came up with an error saying "Property 'tabBar' not found on object of type 'ViewController *'" How do i fix this?
// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]
forState:UIControlStateNormal];
// set selected and unselected icons
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item0.image = [[UIImage imageNamed:#"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item0.selectedImage = [UIImage imageNamed:#"selected-icon.png"];
Instead of using that patch of code, try to do the below in your VC:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"item_unselected.png"]];
Edit:
The code above is used when subclassing UITabBarController, so if you insis you need to subclass the 'UITabBarController', change the class of your tabBarController in the storyboard and put that block of code in its viewDidLoad.
You can delete your UIViewController from storyboard and add there UITabBarController and set it as Initial View Controller.
Then you can access this UITabBarController in AppDelegate.m like this:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
This is the fast way.
Also you can subclass UITabBarController, set this class in the storyboard and put all your code inside this subclass. (as null has said 15 minutes before :)
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:#""]];
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?