I have followed the following tutorial to move my navigation bar down so it is not covered by the status bar in xcode 5/ios7:
Status bar and navigation bar issue in IOS7
But now in iOS7 there is a blank space at the top where the status bar would be and I would like the navigation bar to fill this area too
For instance, Facebook/twittter/Instagram iOS7 apps have the navigation bar background behind the status bar too. How do I achieve this?
Sorry if I'm not being clear but really eager to get this sorted
Thank you!
You do want to set the barPosition of the UINavigationBar.
You can do this in code:
Let your ViewController conform to protocol UINavigationBarDelegate and implement positionBar: metod. (The protocol that you really need is UIBarPositioningDelegate but UINavigationBarDelegate does extend it.)
#interface SampleViewController () <UINavigationBarDelegate>
#property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
#end
#implementation SampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
_navigationBar.delegate = self;
}
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
#end
OR in Storyboard:
In the Identity Inspector of UINavigationBar, add a User Defined runtime Attribute with KeyPath = barPosition, Type = Number, and Value = 3:
If you want to stretch a UINavigationBar with a custom background-image behind the UIStatusBar in iOS 7 consider the following:
The UIStatusBar is transparent as it is.
Set the barPosition property of the UINavigationBar to UIBarPositionTopAttached
The UINavigationBar background-images in iOS 7 (if UIBarPositionTopAttached) have different dimensions than previous to iOS 7 and you have to use them: now the height is 64 points
In code (iPhone ONLY):
// Image needs 64 points height
NSString* navBarPortraitBackgroundPath = [[NSBundle mainBundle] pathForResource:#"navBarPortraitBackground" ofType:#"png"];
NSString* navBarLandscapeBackgroundPath;
if(UIScreen.mainScreen.bounds.size.height == 568){
// Image needs 64 points height
navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:#"navBarWideLandscapeBackground" ofType:#"png"];
} else {
// Image needs 64 points height
navBarLandscapeBackgroundPath = [[NSBundle mainBundle] pathForResource:#"navBarLandscapeBackground" ofType:#"png"];
}
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarPortraitBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:navBarLandscapeBackgroundPath] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsLandscapePhone];
If you just want to change to background colour of the UINavigationBar it will automatically extend behind the UIStatusBar.
In Code:
[UINavigationBar appearance].barTintColor = [UIColor redColor];
Related
While it was pretty easy to set it up in Objective C,
I find it difficult with Swift.
I'm having a Page View Controller which leads me to a Tab Controller (kind of tutorial).
I'm trying to change the selected and unselected to my two images I've got in the Tab Bar, when selected It will chose "imageSelected.png" when not selected it will go with "imageNotSelected.png" (check the snippet code below)
Trying this snippet of code not doing anything
tabBarItem.selectedImage = UIImage(named: "someImage.png")!.imageWithRenderingMode(.AlwaysOriginal)
Any help would be appreciated.
Edit: This is the Objective C I used in my older project which do work.
UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
tabBarController.selectedIndex = 1; // SELTECT INDEX OF TAB BAR
UITabBar *tabBar=tabBarController.tabBar;
UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0]; //first tab bar
UITabBarItem *tabBarItem2=[[tabBar items] objectAtIndex:1];
UIImage *tabrBarImageOne = [[UIImage imageNamed:#“imageSelectedOne.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabrBarImageOneOff = [[UIImage imageNamed:#“imageNotSelectedOne.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabBarImageTwo = [[UIImage imageNamed:#“imageSelectedTwo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabBarImageTwooff = [[UIImage imageNamed:#“imageNotSelectedTwo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1 = [tabBarItem1 initWithTitle:#“title1” image:tabrBarImageOneOff selectedImage:tabrBarImageOne];
tabBarItem2 = [tabBarItem2 initWithTitle:#“title2” image:tabBarImageTwooff selectedImage:tabBarImageTwo];
Note: I've used it in the Objective C code in app delegate which now it's different since I need to load the code differently in each of the the VC I load in the ViewWillAppear.
You'd want to use .AlwaysTemplate instead if you want the tintColor to take effect.
I am trying to set the UINavigationBar background image:
I added in AppDelegate: (please note that it's one image)
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"general-top_bar_with_status.png"] forBarMetrics:UIBarMetricsDefault];
Since I don't need translucent, in the ViewController in ViewDidLoad I added:
self.navigationController.navigationBar.translucent = NO;
This is the image:
Unfortunately this is what I get:
As you can see, the image is upsite down.
What is wrong?
In storyboard, I set a place for TopBar - Opaque navigation bar
For iOS 7 you have to use 320x64 size navigation bar image
make picture 2X Size. with name mynavbar#2x.png
Nav bar is Taking size of its self + status bar size. You may create only navbar size
640X88
or 640X128 for navbar + status bar
Then
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"mynavbar.png"] forBarMetrics:UIBarMetricsDefault];
I want to replace the current nav bar with a custom image. How my code is structured is that a tab bar controller controls a bunch of navigation controllers which contain views (tab bar controller -> nav controller -> view). I tried using this code in my app delegate
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *img = [UIImage imageNamed: #"nav.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
But it did not work. Any ideas why? Should I have placed it somewhere else? Thanks!
if you are using iOS 5, you can use setBackgroundImage: like this where ever you initialized the navigation controller (aNavigationController in this example):
[aNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
I would not use a category. I would subclass UINavigationBar instead. See this: https://stackoverflow.com/a/6959354/472344
If you are targeting iOS 5 or up, use #BJH's solution instead.
I am a beginner at Obj-C/Cocoa Touch/iPhone OS.
I wish to have a background for my app with different images everytime the the view is called.
Say I have 10 images. I 've used it like this:
//random image generation
NSString* imageName;
int aRandomNumber = arc4random() % 10;
imageName =[NSString stringWithFormat:#"g%d.jpg",aRandomNumber];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
NSLog(#"aRandomNumber is %d", aRandomNumber);
//random image is generated
Its working fine
Now, say I have text labels on my view and the text isn't displaying correctly due to image colors.
How can I make it a little transparent? (I guess in Interface Builder its called alpha.)
Say my image isn't 320x480. How do I set it to fill the entire view?
How can I do it with UIView/UIImageView?
I found initWithHue:saturation:brightness:alpha: in the documentation but it's not working:
self.view.backgroundColor = [[UIColor alloc] initWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];
Please Help!
A friend suggested........
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
..........he told it's more efficient because it doesn't save the image in the cache.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
more info with example project
Besides all of the other responses here, I really don't think that using backgroundColor in this way is the proper way to do things. Personally, I would create a UIImageView and insert it into your view hierarchy. You can either insert it into your top view and push it all the way to the back with sendSubviewToBack: or you can make the UIImageView the parent view.
I wouldn't worry about things like how efficient each implementation is at this point because unless you actually see an issue, it really doesn't matter. Your first priority for now should be writing code that you can understand and can easily be changed. Creating a UIColor to use as your background image isn't the clearest method of doing this.
use this
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default"]];
simple way :
-(void) viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.png"]];
[super viewDidLoad];
}
It's a very bad idea to directly display any text on an irregular and ever changing background. No matter what you do, some of the time the text will be hard to read.
The best design would be to have the labels on a constant background with the images changing behind that.
You can set the labels background color from clear to white and set the from alpha to 50.0 you get a nice translucent effect. The only problem is that the label's background is a stark rectangle.
To get a label with a background with rounded corners you can use a button with user interaction disabled but the user might mistake that for a button.
The best method would be to create image of the label background you want and then put that in an imageview and put the label with the default transparent background onto of that.
Plain UIViews do not have an image background. Instead, you should make a UIImageView your main view and then rotate the images though its image property. If you set the UIImageView's mode to "Scale to fit" it will scale any image to fit the bounds of the view.
You want the background color of your main view to be semi-transparent? There's nothing behind it... so nothing will really happen however:
If you want to modify the alpha of any view, use the alpha property:
UIView *someView = [[UIView alloc] init];
...
someView.alpha = 0.8f; //Sets the opacity to 80%
...
Views themselves have the alpha transparency, not just UIColor.
But since your problem is that you can't read text on top of the images... either:
[DESIGN] Reconsider the design/placement of the images. Are they necessary as background images? What about the placement of the labels?
[CODE] It's not exactly the best solution, but what you could do is create a UIView whose frame takes up the entire page and add some alpha transparency to it. This will create an "overlay" of sorts.
UIView *overlay = [[[UIView alloc] init] autorelease];
overlay.frame = self.view.bounds;
overlay.alpha = 0.2f;
[self.view addSubview:overlay];
... Add the rest of the views
You can set multiple background image in every view using custom method as below.
make plist for every theam with background image name and other color
#import <Foundation/Foundation.h>
#interface ThemeManager : NSObject
#property (nonatomic,strong) NSDictionary*styles;
+ (ThemeManager *)sharedManager;
-(void)selectTheme;
#end
#import "ThemeManager.h"
#implementation ThemeManager
#synthesize styles;
+ (ThemeManager *)sharedManager
{
static ThemeManager *sharedManager = nil;
if (sharedManager == nil)
{
sharedManager = [[ThemeManager alloc] init];
}
[sharedManager selectTheme];
return sharedManager;
}
- (id)init
{
if ((self = [super init]))
{
}
return self;
}
-(void)selectTheme{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *themeName = [defaults objectForKey:#"AppTheme"] ?: #"DefaultTheam";
NSString *path = [[NSBundle mainBundle] pathForResource:themeName ofType:#"plist"];
self.styles = [NSDictionary dictionaryWithContentsOfFile:path];
}
#end
Can use this via
NSDictionary *styles = [ThemeManager sharedManager].styles;
NSString *imageName = [styles objectForKey:#"backgroundImage"];
[imgViewBackGround setImage:[UIImage imageNamed:imageName]];
I have tried this approach/hack:
http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/
The problem is this leaves a faint seam. I tried setting the background image of the nested toolbar to an image I captured of what it should be. That didn't work. The image was not applied. I have also tried using a nested UINavigationBar and that didn't seem to work.
I have seen this done in several iPhone apps. Does anyone know how?
[EDIT] I want the buttons to look like normal UIBarButtonItems and be able to use system styles like UIBarButtonSystemItemAdd, UIBarButtonSystemItemRefresh. The link I provided does this except you can see a faint seam because it is a UIToolbar nested in the navigationbar..
Please don't mention this breaking the Human Interface Guidelines. (We know).
I appreciate you contributing your hacks... thats the only way to do this!
iOS 5.0 now supports multiple buttons. See the iOS documentation for UINavigationItem. Specifically, the following:
Properties:
#property(nonatomic, copy) NSArray *leftBarButtonItems;
#property(nonatomic, copy) NSArray *rightBarButtonItems;
#property BOOL leftItemsSupplementBackButton;
Methods:
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;
I posted code to add two buttons to the right of the navigationBar. You can set barStyle = -1 instead of subclassing UIToolbar.
To get rid of the background ('seam') of a UIToolbar, create a subclass of UIToolbar and override the (void)drawRect:(CGRect)rect method. Leave that blank and your UIToolbar will no longer have a background.
Just used this in my own project and worked great. Found this in the comments of: http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];
see uicatalogue example available at apple's site for free...they used uisegmented control to show three buttons in place of right bar button on navigaion bar...
I can't comment but in addition to #iworkinprogress I had to set the UIToolbar background color to clear:
[toolbar setBackgroundColor:[UIColor clearColor]];
This was also found in the comments of http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html.
In iOS 4.x the clearColor seems to have no effect on the UIToolbar, whereas overriding its drawRect: did.
I came up with a helper function I'm using all over my project. Basically it checks if there is already a button on the bar and either add the new one or merge it with existing buttons. So you can call the function just once or multiple times:
+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
UIButton *newButton =[[UIButton alloc] init];
[newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
newButton.frame = frame;
[newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];
if ([[controller.navigationItem rightBarButtonItems] count] == 0)
[controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
else {
NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
[existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
[controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
}
}
Call it from the view controller:
[Helper AddButtonToBar:self withImage:#"imageName.png" withAction:#selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];