UINavigationbar with a custom background image - iphone

I have a custom category to define a special background image for my navigationbar depending on the view I'm in.
Within the category I set the image using
- (void)drawRect:(CGRect)rect
The thing is, when navigating through my views in the Navigation Controller, the custom image disappears leaving a blank white space 320x44(size of the bar) until the next controller reappers - i.e. after about 1 second.
This sort of looks ugly and I was wondering whether there was a better way to do this so I have a clean implementation which looks nice aswell and fits my demands.
Thanks in advance.
Category Code:
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
UIImage *imageName = [navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
UIImage *image = imageName;
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
- (void)setMyImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
[self setNeedsDisplay];
}
Then I call:
[self.navigationBar performSelectorInBackground:#selector(setMyImage:) withObject:[UIImage imageNamed:imageTopbar]];

You should use UIKit only on the main thread.

In your ViewController, try this where #"BGimage.png" is your image file:
//puts an image as bg pattern(tiles) for a View
//self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BGimage.png"]];

Related

annotationView override drawRect, image with alpha

Hello i want to override drawrect in my custom annotationView, so when i write
[[_mapView viewForAnnotation:annotation] setNeedsDisplay];
my annotation view will be redrawn and i wouldn't have to remove the annotation and add it again.
here is my drawRect
- (void)drawRect:(CGRect)rect {
UIImage* theImage = nil;
if( _pinType == T_UNKNOWN ) theImage = [UIImage imageNamed:#"imgU.png"];
else theImage = [UIImage imageNamed:#"imgK.png"];
[theImage drawInRect:rect];
}
The problem is that my images are with alpha and the alpha part is black.
So maybe anyone knows the solution or some suggestions to this?
I've read a lot of post about this, also using core graphics, but didn't find the solution..
Thanks in advance!
Do you want this view to be partially transparent and display things under it? If so, use [self setOpaque:NO]. An opaque view's drawRect is responsible for drawing every pixel in the rectangle with a fully opaque color.
This function will be work correct for iOS 5.0. When you will use iOS version < 5.0, you'll got alpha part as black.
To try use another method for draw your images. I don't know for what you use this code. To try use:
UIImageView *image = [[UIImageView alloc] initWithImage: theImage];
image.opaque = NO;
[self.view addSubview: image];

iOS Dynamic Change UINavigationBar's Background With Image

I have read this and change UINavigationBar's background:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"top-with-logo.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
you see, I add a image named top-with-logo.png, and I want to change the background view by adding an image named top-without-logo.png. But the code above will change the background forever and i cannont change any more. Do you know how?
Thank you very much.
I'm not sure I understand what you mean, but you can place a Boolean ivar on your app's delegate. then in the function you load top-with-logo.png if it's true/false and load top-without-logo.png if it's the other value. then, when you want to change the logo, just change the Boolean's value and call setNeedsDisplay on the navigationBar.
if you use xib files, you can also subclass UINavigationBar, and instead of using a catagory you override the drawRect function and place the variable as an instance variable as a subclass, I believe it is cleaner, but you can only set that up in Interface builder ( otherwise there's no way to tell a UINavigationController to use a different class for the navigationBar property )
You can use,
UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: #"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];

Custom UINavigationController UINavigationBar

Basically I want a custom UINavigationBar. I don't want it to be "translucent" or anything, like the pictures app.
I basically want to completely remove it, but I still want to be able to add back buttons and such when navigation controllers are pushed, and I want the views (EG: UITableViewController) to be pushed down below it.
Like this:
Any ideas how to achieve this at all?
Thanks
#implementation UINavigationBar (background)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
basically, its not completely see through - its a visual lie. The only way to do it realistically is to override UINavigationBar's drawRect: method, as shown above.
To see through the UINavigationBar, if you choose to have one, just:
self.navigationController.navigationBar.translucent=YES;
You'll have to change the tint/color to match the background if you want it to appear like the image you posted.
At the beginning of your AppDelegate subclass UINavigationBar as below:
#interface CustomNavBar : UINavigationBar
#end
#implementation CustomNavBar
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
and then in the AppDelegate do this magic:
//Set custom NavigationBar
[self.navController setValue:[[CustomNavBar alloc]init] forKeyPath:#"navigationBar"];
//Set tint to match bar.png
self.navController.navigationBar.tintColor = [UIColor colorWithRed:0.93 green:0.43 blue:0 alpha:1];
//Set font for NavigationBar
[self.navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Comfortaa-Bold" size:20], UITextAttributeFont, nil]];
That should give you a lot more control over UINavigationController look & feel.
Hard to tell, could be the UINavigationBar is there and color matches the UIView background or, there is no UINavigationBar, just a view with custom buttons and UILabel on top. Pick an approach and code it, or ask the question again with more specifics.

iPhone UINavigationController problems (invisible Button, title)

I have two problems with the UINavigationController.
Problem 1
On the initial view I programmatically added a rightBarButtonItem.
When I push the navigationView to the right, everything works right and it appears a back button with the title of the previous view.
When I click the back button, I get back. But then this button is gone. But it's not really gone, it's just invisible, because it can still be clicked.
Problem 2
When I push the viewController on the right, I can't set the title. I tried it before pushing the view with
myViewController.title = #"someTitle";
and I tried it also inside of the viewController with
self.title = #"someTitle";
but nothing works here.
I found the problem:
I was adding a custom background image to the navigationBar. Somehow this image got over the title / button.
So if you ever want to implement a custom background image, don't user this:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[backgroundImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"headerBg" ofType:#"png"]]];
[navigationController.navigationBar insertSubview:backgroundImage atIndex:0];
[backgroundImage release];
But instead, use this:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"headerBg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Problem 1:
Sounds odd. Don't really have an answer for that :-/
Problem 2:
Can you provide some more code snippets on this, like the whole method bodies where you tried to set the title?

UIImageView subclass does not display

I am using a custom subclass of UIImageView, but I can't figure out why it's not being displayed.
The relevant code from my UIImageView subclass:
-(id) initWithImage:(UIImage*)image{
if(self = [super initWithImage:image]){
}
return self;
}
And from the view controller for the view that will be displaying my subclass:
UIImage *zoomRatateSliderHorizontal =
[[UIImage imageNamed:#"ZoomRotate Slider Horizontal.png"]
stretchableImageWithLeftCapWidth:(75.0)/2-1.0 topCapHeight:0];
scaleHorizontalControl = [[ViewTransformationController alloc]
initWithImage:zoomRatateSliderHorizontal];
scaleHorizontalControl.onScreenFrame = CGRectMake(0.0, 5.0,
self.view.frame.size.width,
scaleHorizontalControl.image.size.height);
scaleHorizontalControl.frame = scaleHorizontalControl.onScreenFrame;
scaleHorizontalControl.offScreenFrame = CGRectZero;
[self.view addSubview:scaleHorizontalControl];
Before I was subclassing, I had no trouble with the following in the view controller:
UIImage *zoomRatateSliderVertical =
[[UIImage imageNamed:#"ZoomRotate Slider Vertical.png"]
stretchableImageWithLeftCapWidth:0 topCapHeight:(75.0)/2-1.0];
scaleOrRatateViewVertical = [[UIImageView alloc]
initWithImage:zoomRatateSliderVertical];
scaleOrRatateViewVertical.frame = CGRectMake(-zoomRatateSliderVertical.size.width,
zoomRatateSliderHorizontal.size.height+5.0+5.0,
zoomRatateSliderVertical.size.width,
465.0 - zoomRatateSliderHorizontal.size.height-10.0-5.0);
[self.view addSubview:scaleOrRatateViewVertical];
Using break points I checked the frame and image being passe to my class, and they both appear to be valid and desired.
Any advice on what I'm doing wrong I'd greatly appreciate.
The Apple Docs read:
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
So I'd subclass UIView and work from there.