I'm trying to create a custom UINavigationBar with a custom shape, like this (ignore transparency)
As you can see, this UINavigationBar has a custom shape and I'm trying to replicate it.
Looking around I found this response, where it explains the firsts steps I followed.
1) I created a subclass of UINavigationBar called CustomNavigationBar. 2) I overrode the sizeThatFits method like this:
- (CGSize) sizeThatFits:(CGSize)size
{
return CGSizeMake(320.0, 70.0);
}
3) And here is where I'm lost...
In the previous answer is said that UIBezierPath can be used to create a custom shape (even with curves) and then applied as a mask. I tried this overriding drawRect but all i get is a big black navigation bar (my bar color is set to red).
EDIT: my draw was wrong, this is the correct one
- (void)drawRect:(CGRect)rect
{
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(320.0, 0.0)];
[path addLineToPoint:CGPointMake(320.0, 50.0)];
[path addQuadCurveToPoint:CGPointMake(0.0, 50.0) controlPoint:CGPointMake(160.0, 90.0)];
[path closePath];
[[UIColor redColor] setFill];
[path fill];
}
EDIT: As said below, my code had some errors and now it draws something.
As you can see, the UIBezierPath defines the shape correctly but, there is some new problems:
1) The status bar is completely black, nothing is rendered there, even if I change his color to light, it doesn't show nothing. What am I missing?
2) Due to the sizeThatFits method, there's still some black part left. Is there any way to make that part transparent?
Thanks to all!
EDIT 2: well, I totally changed my perspective about this issue and I think i'm getting close to a solution. Now I'm trying to use a transparent png file as background, but still need to increase it's height, so right now this is my code.
- (CGSize) sizeThatFits:(CGSize)size
{
return [[UIImage imageNamed:@"Layer3"] size];
}
- (void)drawRect:(CGRect)rect
{
[self setClipsToBounds:NO];
UIImage *image = [UIImage imageNamed:@"Layer3"];
[image drawInRect:rect];
}
Simpler ,right? Obviously "Layer3" is the name of my transparent png image. But now, this is what I'm getting.
As you can see, the status bar is not covered with the png image.
What am i missing now?
Thanks!