IOS: Unable to Add Shadow To UINavigationBar - iphone

Hey I am trying to add a shadow to all UINavigationBars in my project. I have tried a number of things recommended on this site and on other internet tutorials but have had no luck in getting a shadow to stick. I have tried putting it in using a shadow image I have created in photoshop, and calling the following in the main appDelegate:
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"shadowTest.png"]];
I have subclassed the UINavigationBar out and tried to assign the property from within the class inside the awakeFromNib method and still nothing. I then tried to manually create the shadow with <QuartzCore/QuartzCore.h> image framework but nothing happened. I am using the subclass to override the drawRect method, could this be causing my problem? Here is the UINavigationBar class with my latest attempt at getting the shadow to stick.
#import "BasicNavigationBar.h"
#implementation BasicNavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
// draw shadow
self.layer.masksToBounds = NO;
self.layer.shadowOffset = CGSizeMake(0, 3);
self.layer.shadowOpacity = 1.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rect);
}
#end

setShadowImage doesn't work if you don't set the backgroundImage, too.
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

Related

cornerRadius not working if replacing default view with custom subclass

Following setup:
i have a popupViewController that has a custom UIView subclass as its view, done in loadView
- (void)loadView
{
CGRect startingPopupSize = CGRectMake(0.0f, 0.0f, 300.0f, 160.0f);
_popupView = [[MoviePopupView alloc] initWithFrame:startingPopupSize];
self.view = _popupView;
}
in this UIView Subclass i have the following code in it's init method
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.layer.masksToBounds = YES;
self.layer.opaque = NO;
self.clipsToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.layer.borderWidth = 1.0f;
self.layer.cornerRadius = 10.0f;
}
return self;
}
the problem is, the cornerRadius does not work for this view, but the border gets drawn, see here:
if i don't replace this view with the default uiview of the uiviewcontroller and instead add it as a subview, the cornerRadius works just fine (i want to replace it for several reasons).
any ideas why this is not working?
Edit:
if i just move the layers masksToBounds = YES property from the initWithFrame Method to the drawRect method it works. moving it to its viewcontrollers viewDidLoad doesn't.
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.layer.masksToBounds = YES;
}
any ideas why this works? i think it is not the right place to set this property here.
You should make two views:
background view
text view
For background view set color you want for your text background.
For text view set background color as clear color.

UIView drawInRect with UIImage results in blackbackground

I have this following codes:
#implementation MyImageView
#synthesize image; //image is a UIImage
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void) removeFromSuperview
{
self.image = nil;
[super removeFromSuperview];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.image)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
//the below 2 lines is to prove that the alpha channel of my UIImage is indeed drawn
//CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
//CGContextFillRect(context, rect);
CGContextDrawImage(context, self.bounds, self.image.CGImage);
}
}
#end
When I ran the code, I realized that the background of my view is black. To test if it was a problem with my UIImage, I used the 2 lines commented after CGContextClearRect(context, rect). Indeed a white background was drawn. Is there anyway for me to remove the black background? When I init MyImageView, i have already set backgroundColor to [UIColor clearColor].
Any advice is much appreciated.
Thanks
Setting the background color to [UIColor clearColor] should work. Also set self.opaque = NO to enable transparency.
You should also check that the correct initializer is being called. For example if the view is part of a XIB file, you need to implement initWithCoder: as well as initWithFrame: etc.
I had the same issue - this worked for me (Swift example)
var backgroundImage = UIImage() //background image - size = 100x100
var frontImage = UIImage() //smaller image to be drawn in the middle
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), false, 0.0) //false here indicates transparent
var context = UIGraphicsGetCurrentContext()!
UIGraphicsPushContext(context)
backgroundImage.drawInRect(CGRectMake(0, 0, 100, 100))
frontImage.drawInRect(CGRectMake((100 - frontImage.size.width) / 2, (diameter - frontImage.size.height) / 2, frontImage.size.width, frontImage.size.height))
UIGraphicsPopContext()
var outputImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Your CGContext will draw upside down
anyhow, so just go the easy route and use
[self.image drawInRect:CGRectMake(rect)];
Instead.

Transparent background in CoreGraphics

I am trying to get a transparent background in CG but it keeps coming out black.
I have the following code:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
DLog(#">>> Alloc: DrawingCanvas [0x%X]", (unsigned int)self);
[self setOpaque:NO];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect frame = rect;
int counter = 3;
CGContextClearRect(context, frame);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, frame);
}
How do I get this code to show a transparent background?
With that setup and as long as you don't set clearsContextBeforeDrawing to NO, the background should already be transparent when your drawRect: method is called. Remove the CGContextClearRect and other drawing calls.
You could simply remove these lines :
CGContextClearRect(context, frame);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, frame);
As long as you have set opaque to NO and clearColor as backgroundColor everything should be fine.
Be careful when drawing since your other drawing code may fill the background entirely (take care of [path fill] things in a non-clipped context).
I had this same problem and the solution was to set the backgroundColor property of the UIView which I was drawing in, from the parent view's class. I didn't need any code to clear the background in the UIView's class where the drawing happens.
myDrawingView.backgroundColor = [UIColor clearColor];

iphone sdk transparent subview background

I have a main view with a picture on it.
I am trying to add a subview with [self.view addSubview:view2]; but I want the view2 background to be transparent. Have tried opaque=no and background color to clearcolor and also tried to subclass a uiview and rewrite the drawrect with:
#import "TransparentView.h"
#implementation TransparentView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];
self.opaque=NO;
self.clearsContextBeforeDrawing=YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
}
#end
But still doesn't display the background of the subview transparent... any ideas?
Try:
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
Is the view being loaded from a nib file? If so, the -initWithFrame: won't be called; -initWithCoder: will be called instead. A better place to do this initialization might be in -viewDidLoad. But setting the background color to [UIColor clearColor] should definitely do the trick.
Try coloring the subview's background with a 0.0 for Alpha. That should make it completely transparent.
Something like this:
UIColor *myUIColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha:0.0];
In the function
- (void)drawRect:(CGRect)rect
try to update
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
to
const CGFloat BACKGROUND_OPACITY = 0.85; //Note: update this value to what you need
CGContextSetRGBFillColor(context, 1, 1, 1, BACKGROUND_OPACITY); // You can change 1,1,1 to the needed values
This link might help you
http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
I've had some cases where ... addSubview:clearView] seemed to reset the background color of clearView (WTF!) to something not clear. I added a
[clearView setBackgroundColor:nil];
somewhere after that and it seemed to help.

How to make a subview tint a portion of the screen on the iPhone?

I would like to write a UIView subclass that, among other things, colors whatever's underneath it a certain color. This is what I've come up with, but it unfortunately doesn't seem to work correctly:
#import <UIKit/UIKit.h>
#class MyOtherView;
#interface MyView : UIView
{
MyOtherView *subview;
}
#end
#implementation MyView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
frame.origin.x += 100.0;
frame.origin.y += 100.0;
frame.size.width = frame.size.height = 200.0;
subview = [[MyOtherView alloc] initWithFrame:frame];
[self addSubview:subview];
[subview release];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Draw a background to test out on
UIImage *image = [UIImage imageNamed:#"somepic.png"];
[image drawAtPoint:rect.origin];
const CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blueColor] setFill];
rect.size.width = rect.size.height = 200.0;
CGContextFillRect(ctx, rect);
}
#end
#interface MyOtherView : UIView
#end
#implementation MyOtherView
- (void)drawRect:(CGRect)rect
{
// This should tint "MyView" but it doesn't.
const CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeScreen);
[[UIColor redColor] setFill];
CGContextFillRect(ctx, rect);
CGContextRestoreGState(ctx);
}
#end
I want "MyOtherView" to color "MyView" red where it overlaps, but instead it just draws an opaque red block onto it. However, this seems work fine if I copy the -drawRect: function from "MyOtherView" and append it to the one in "MyView" (this took me quite a headache to finally realize). Anyone know what I'm doing wrong? Is it even possible to do this, or should I be approaching it differently?
I think you're over-thinking this. Overlay one view over the other, and set the alpha of the top view to 0.5. You will also need to set opaque to NO.
If you set the background color of the view appropriately, you won't even need to override drawRect.
Might want to check out this SO question.