iphone: setting background image in navigationbar is not fitting - iphone

i have a table view that i want to make a custom uinavigationn bar i tried this code
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
//here for v, width= navBar width and height=navBar height
//
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"newsBanner.png"]]];
[self.navigationController.navigationBar addSubview:view];
the problem is the image is not centered cause its size is 640 x 72... is there a way to make it fit?

Your nav bar should only be 44 pixels tall. On top of that, it is maximum 320 points wide (640 px on retina devices, 320 on non-HD devices). So the size of your image is way too big. Make an image that has 320x44 dimensions, and then do this:
UINavigationBar *theBar = self.navigationController.navigationBar;
if ( [theBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"your image here"];
[theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
Of course that will only work on iOS 5. You'll need to find other ways of handling setting the background on OS below 5.0. But there are plenty of stack overflow questions that address that issue :)

Related

Status bar color not changing according navigation bar's color

I confronted with the problem. I have a project both on iOS6 and iOS7. In whole app I'm using UINavigationBars. But my application's status bar doesn't change his color according my navigation bar's background image. My code of navigation bar's customisation:
- (void)setBackgroundImageForNavigationBar:(UINavigationBar *)navBar {
UIImage *backgroundImage = [UIImage imageNamed:#"navigationBackgroundImage"];
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:[[UIImage alloc] init]];
}
For iOS6 it's work fine. How can I resolve this problem for iOS7?
Your image needs to be 66 pixels (or the #2x version 132). If it is 44, you will still get the little bar up top.

set background images for Iphone 3g, Iphone 4s and Iphone 5

I want to set the background image of a view. I use three images: backgroundimage.png (size 320 x 480), backgroundimage#2x.png (size 640 x 960) and backgroundimage-568h#2x.png (size 640 x 1136)
that works for iphone 3g size. But when I test it for retina size the background image is just a small part of the original image. I think, that I have to downscale the image somehow, or set a frame for it, but I just don't know how.
Thanks for any help ;)
thats the code:
self.view.backgroundColor = [UIColor clearColor];
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
backgroundImage = [UIImage imageNamed:#"background-568h#2x"];
}
else{
backgroundImage = [UIImage imageNamed:#"background"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
My first guess is because of the misuse of colorWithPatternImage. This should only be used if you have a pattern image that you wish to have tiled. Instead try creating a UIImageView the size of the view, and then adding it as a subview of self.view.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];

Setting Background Image of UITableView - White band appearing at bottom

When I use the following code to set the background of my UITableView to an image, about 20-30px of the image does not draw at the very bottom of the screen. I'm very puzzled by this as the image size is 480 high while the view is 460 high. The image should actually be sizing down to just barely fit the screen. Could somebody enlighten me? Thanks a lot
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]];
self.tableView.backgroundView = imageView;
Set the frame of the background image to this:
{{0, -20}, {320, 480}} either in IB or in code.
ie
imageView.frame = CGRectMake(0, -20, 320, 480);
the high of of all the screen is 480 px and the statusBar has 20 px so your view has 460 px

iPhone: Mask UIImageView of differing dimensions into square dimension

I have a bunch of UIImageViews that are in different proportions. Some of 100x101 some are 130x121.
How can I mask these to 80x80 and NOT stretch the images? I basically just want to mask a square out of each one. (kind of like the Apple's Photo thumbnail view does)
Set the image view's size to 80 x 80
set the image view's contentMode property to UIViewContentModeScaleAspectFill
Finally, to make round corners, use the following code, and import QuartzCore/QuartzCore.h at the beginning of your implementation file.
CALayer * layer = [myImageView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:12.0f];
Edited: Yes, by saying size I mean frame, the W and H:
Set its content mode UIViewContentMode, you may be looking for UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:#"myImage.png"];
.
.
.

using image or tint color on uinavigationbar in iphone?

how do i show a background image on a navigation bar or give tint color to the navigation bar in a native iphone application??
For iOS5 use the following lines of code:
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:#"nav-bar-background-normal"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
For backward compatibility do a check to see if the navigation bar responds to setBackgroundImage:forBarMetrics:
More information on:
http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
This's how I did on iOS4:
#import <QuartzCore/QuartzCore.h> // For .layer
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"navigationBarBackgroundImage"].CGImage;
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
No need to switch subviews between z-orders (-exchangeSubviewAtIndex:withSubviewAtIndex:), both background image and tintColor set in one line of code, and works with #2x image too.
Was looking for this a week ago. Found this over here discussions. apple. com/thread.jspa?threadID=1649012&tstart=0 (sorry won't let me post a real link).
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{
if(image == NULL){ //might be called with NULL argument
return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
/* input: The tag you chose to identify the view */
-(void)resetBackground:(NSInteger)bgTag {
[self sendSubviewToBack:[self viewWithTag:bgTag]];
}
I made this as a category to UINavigationBar. To set it a background image for a UINavigationBar inside a UINavigationBarController, I did this:
[navigationControllerForChannels.navigationBar setBackgroundImage:[UIImage imageNamed:#"top_bar.png"] withTag:48151623];
I've had some buginess when updating the tab bar, so you'll want to call
[self.navigationController.navigationBar resetBackground:48151623];
After any modifications to the bar.
You can override UINavigationBar drawRect. The code can be placed to appDelegate.m
I've tested it and it's working on 3x and 4x iOS.
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor]; //tint color
UIImage *img = [UIImage imageNamed: #"navBarBg.png"]; // your image
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}#end
For the iOS5 and iOS6 I've used this solutions and it worked perfectly, Making a Universal UINavigationBar Background Image.
iPhone Retina Portrait 640px x 88px / iPhone Non-Retina Portrait 320px x 44px
Inside AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Place this code
// Set the status bar to black color.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
// Change #"menubar.png" to the file name of your image.
UIImage *navBar = [UIImage imageNamed:#"menubar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBar forBarMetrics:UIBarMetricsDefault];
Don't forget to change the image name (menubar.png)
Check out this link for the full answer http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/
a background image is going to take a bit more work (you might want to try setting a titleView that's the same size as the bar itself; I haven't tried this myself) or adding a view behind existing subviews. Tint color is easy: navBar.tintColor = [UIColor orangeColor];
If you use the CGImage solution, you may have a problem with image size:
CGRect layer=self.navigationController.navigationBar.frame;
layer.size.height=57.0;
layer.origin.y=0;
self.navigationController.navigationBar.frame=layer;
CGImageRef imageRef = [UIImage imageNamed:#"myImg.png"].CGImage;
self.navigationController.navigationBar.layer.contents = (id)imageRef;
It seems to me that the image is streched down, since the layer seems to have a height of 44.0 pixel, but a background image for the UINavigationBar should be at least 57.0.
If you try to move the layer's frame, all the buttons will move within it.