I've got a UIView container with a table view in it, whose cells contain images and buttons.
I want to add a shadow to the outter-most container but when I do, I found shadows are also added to all the images and buttons. How can I only add shadow to the layer itself without its sublayers?
code:
listContainer.layer.shadowColor = [UIColor blackColor].CGColor;
listContainer.layer.shadowOffset = CGSizeMake(3, 0);
listContainer.layer.shadowOpacity = .8;
listContainer.layer.borderColor = [UIColor blackColor].CGColor;
Thanks!
Have you tried the shadowPath property of your layer?
listContainer.layer.shadowPath = [UIBezierPath bezierPathWithRect:listContainer.bounds.CGPath];
I had this problem once and the issue was that the layers background color was transparent.
So if you have a UIView, or CALayer, and you are trying to set shadows on it, make sure that there is a background color to prevent shadows from being applied to the sublayers.
Related
I want to add border for an image. I don't want to add it completely on 4 sides. I want to add border only on particular 3 sides i.e., top,left & bottom. How to do that?
[imageView.layer setBorderColor:[[UIColor clearColor] CGColor]];
This would set the border on 4 sides of the image. But I want it only on 3 sides. How to do that?
You’ll need to add colored subviews on the edges that you want to have borders. See this answer—for the border on the top, follow the same pattern, but use an autoresizingMask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin.
Make sure that the UIImageView has its clipsToBounds set to YES. Create a UIView that is the one border width wider than the image. Set the border on that layer to have the desired color. Set the frame of this view to 0,0, imageWidth+border, image.height and set masksToBounds = YES. Add this as a subview to your UIImageView. It should put a border around all but the right hand side.
You could draw a filled CALayer that is 2px higher and 1px wider behind the actual image layer. Set the frame to be 1px above and to the left of your image.
manipulate the view with shadow and this is done easy peasy
_topInfoView.layer.masksToBounds = NO;
_topInfoView.layer.shadowOffset = CGSizeMake(0, 1);
_topInfoView.layer.shadowColor = [[UIColor grayColor]CGColor];
_topInfoView.layer.shadowRadius = 0.27f;
_topInfoView.layer.shadowOpacity = 0.6;
This is my code.
listView.layer.masksToBounds = NO;
listView.layer.shadowOffset = CGSizeMake(-3, 3);
listView.layer.shadowColor=[[UIColor blackColor] CGColor];
listView.layer.shadowRadius = 4;
listView.layer.shadowOpacity = 1.0;
[listView.layer setShouldRasterize:YES];
It works good with shadow effect.
While changing
listView.layer.masksToBounds = YES;
I didnt get shadow effect.
The shadow is actually drawn below the UIView. If you set maskToBounds to YES, this clips any drawing outside of the UIView frame. Here is a SO link that describes this in more detail.
The shadow is drawn outside of the layer's bounds. You have to set listView.layer.masksToBounds = NO to see the shadow
if you set listView.layer.masksToBounds = YES you can't draw anything out side of bounds so you can not get shadow
Only below worked for me
[self.view bringSubviewToFront:subView];
If you have to use masksToBounds = YES; check out this SO post. It tells you how to use both shadows and rounded corners (in this particular case) on a view by using two nested views: the outer view casts the shadow and does not mask to bounds while the inner view has rounded corners and masks to bounds.
I'd like to draw a simple inset line in Interface Builder to separate some items in a list and make the UI a bit more tidy. I don't see any "line" or similar objects in the objects library and can't seem to find any drawing commands in Interface builder.
I use a very narrow UIView with backgroundColor set to the appropriate color.
There are no lines in the iPhone UI library. This functionality on Max OS X was supplied by NSBox, but on the iPhone there is no corresponding UI element.
If you're afraid a UIView might affect performance, you can draw a line in code using CoreGraphics' CAShapeLayers.
Every UIView has a CALayer, so draw the line and add it to the views CALayer.
You can do this either in a custom UIView's drawRect or in your view controller:
(Make sure to add Quartz framework to your project)
UIBezierPath *linePath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0,self.view.frame.size.width, 1)];
//shape layer for the line
CAShapeLayer *line = [CAShapeLayer layer];
line.path = [linePath CGPath];
line.fillColor = [[UIColor blackColor] CGColor];
line.frame = CGRectMake(xPosition, yPosition, self.view.frame.size.width,1);
[self.view.layer addSublayer:line];
In place of view, why not just use a label and set the appropriate background color?
I used a view and made it narrow & changed the color to make it look like a line. But my issue is I am using the line on a vertical scrollview and the lines also get scrolled.
Interface Builder does not allow you to draw the shadows essential for an inset line. If you work with Photoshop you know this.
The following code will draw an "inset" if the line is in front of a white/beige Background.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1.0f)];
line.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:(200.0f/255.0f) blue:(200.0f/255.0f) alpha:1.0f];
line.layer.shadowColor = [UIColor darkGrayColor].CGColor;
line.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
line.layer.shadowRadius = 0.5f;
line.layer.shadowOpacity = 0.4f;
line.layer.masksToBounds =NO;
[Background addSubview:line];
Remember to import < QuartzCore/QuartzCore.h>.
As an alliterative - in Interface Builder, add a view who's height is set to 2 points. Wire up a IBOutlet to that view and set its layer border color and width:
self.mySeparatorLine.layer.borderWidth = 1.0f;
self.mySeparatorLine.layer.borderColor = [UIColor lightGrayColor].CGColor;
You can use Progress View as an option for a horizontal line. Just change the tint color and progress to 1 which is 100%.
I have a UIView with an alpha of 0.5 which I add as a subview to my primary view in order to gray-out everything else. I want to add an additional UIView to this gray UIView as a subview - the problem is that when I do this, my newly-added subview is also partially transparent.
Is there any way to make a subview "ignore" the alpha value of its superview and be itself fully opaque?
Set the UIView background color alpha not it's alpha directly.
Objective-C
UIView *view;
...
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];
It's not the same as:
view.backgroundColor = [UIColor blackColor];
view.alpha = .6;
Swift
view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
No, not really. What you want is to take your overlay view, and make it just have a clear background color. As a subview of that new overlay place your view that will grey things out. And as a sibling view to that put your view you want to be opaque.
[OpaqueView] [DimmingView]
| |
[OverlayView]
Don't put it inside the semi-transparent view. Make it a sibling to semi-transparent view and put it over it using z-ordering.
This will only work if you have any image on the background.
Instead of reducing the alpha of UIView, add an UIImageView on that view and then reduce the alpha of the UIImageView.
now add your subviews on the UIView.
your subviews will not take the alpha property anymore.. :)
No, any view will inherit the opacity of its parent.
So I'm setting UILabel.shadowColor to a non-gray color, but the shadow always appears as opaque 50% gray (or so). For example, I tried setting the shadow to red and I still see gray. Has anyone else seen this? (This is the UILabel inside a custom nav bar back button)
I ran into this same problem while trying to add a non-gray drop shadow to a UIButton's titleLabel. The solution appears to be to set the properties of the button's layer instead:
button.titleLabel.layer.shadowColor = [UIColor whiteColor].CGColor;
button.titleLabel.layer.shadowOffset = CGSizeMake(0, 1);
button.titleLabel.layer.shadowOpacity = 1;
button.titleLabel.layer.shadowRadius = 0;
shadowOpacity is necessary for the effect to appear at all, and shadowRadius has to be set explicitly since the default is 3.0 (very blurry).
This solution requires #import <QuartzCore/QuartzCore.h>.
Are you sure you are not confusing backgroundColor with shadowColor?