I'm trying to add a shadow to a UIButton layer, but for some reason it cuts off at the bounds of the button. I set clipsToBounds is turned off, so I'm not sure why it's looking like this:
It's square, just like that, even though the shadow should not be squared..it should be soft and fading.
button.clipsToBounds = NO;
button.layer.masksToBounds = NO;
button.layer.shadowOffset = CGSizeZero;
button.layer.shadowPath = [UIBezierPath bezierPathWithRect:button.layer.bounds].CGPath;
button.layer.shadowOpacity = 0.7;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowRadius = 10;
Am I missing anything?
Oh, and my button is a subclass of UIButton. I'm not sure if that makes a difference..
-Even with a shadowRadius of 0.0, I get a fully visible black square as my shadow
clipsToBounds will allow any child views to be drawn outside the bounds of your button.
Looks like it doesn't apply to your layer though :(
You might just have to make your button a bit bigger (or your shadow smaller!)
Related
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'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.
I would like to know how Apple built the about view. It looks like that text is inside UITableView element but the whole cell is scrollable.
My guess would be a UIWebView inside a custom table cell.
But that is just a guess. It could be a completely custom view, or various combinations of existing views.
No custom views are needed. All you have to do is configure the text view's layer appropriately. Here's a recipe that produces pretty much the effect you're looking for, assuming you have a UITextView in a view with light gray background:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.clipsToBounds = NO;
CALayer *layer = self.textView.layer;
layer.cornerRadius = 10.0;
layer.borderWidth = 0.5;
layer.borderColor = [[UIColor grayColor] CGColor];
layer.shadowColor = [[UIColor whiteColor] CGColor];
layer.shadowOffset = CGSizeMake(0.0, 1.0);
layer.shadowOpacity = 1.0;
layer.shadowRadius = 0.5;
}
I had some trouble getting the white shadow to display. This SO question explains that you need to set clipsToBounds to NO in order to get the shadow to work.
Here's a picture of the result. I've shown the bottom corner so that you can see the white drop shadow.
Edit: I see now that the view in the question probably is, in fact, a UIWebView. I think it's possible to embed inline images in a NSTextView, but that's probably not the case with UITextView. Anyway, the recipe above should work as well for a UIWebView as it does for UITextView (or any other view).
You can achieve this with a stock UITextView; it's a subclass of UIScrollView, so you can just add the logo imageview as a subview. Then, make room for the image on top by adjusting the text padding:
textView.contentInset = UIEdgeInsetsMake(80,0,0,0);
If you have a tableview that has one section, one row, and the row has a view (UILabel or UITTextField) that is larger than the visible area on the screen, that would scroll like that. Or maybe just a UIScrollView with a UILabel in it.
I am trying to add round corner to UISearchBar at the top-left and top-right corners.
However, I found the mask layer doesn't work on UISearchBar, no matter how I set it.
Here is my code
// UIView* bar = [searchBar_.subviews objectAtIndex:0]; // try to add mask to background view but also failed
UIView* bar = searchBar_;
CGRect toolbarBounds = bar.bounds;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect: toolbarBounds
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(5.0f, 5.0f)];
[maskLayer setPath:[path CGPath]];
[maskLayer setFillColor:[[UIColor greenColor] CGColor]];
bar.layer.masksToBounds = YES;
bar.layer.mask = maskLayer;
I have also tried the "Clip subviews", but it doesn't work either.
Then I try something more, I just set the background view (subview 0) hidden. To mu surprise, it is still visible.
Is there any magic in the UISearchBar?
The mask does work on UIToolbar.
Your problem could be related to how the mask property is implemented. It will not work if you add a mask layer to a layer which is already part of a hierarchy. The docs for CALayer say:
When setting the mask to a new layer, the new layer’s superlayer must first be set to nil, otherwise the behavior is undefined.
You'll have to remove the search bar from its superview before adding the mask layer. Wrap your last line like so:
UIView *superview = [bar superview];
[bar removeFromSuperview];
bar.layer.mask = maskLayer;
[superview addSubview:bar];
You can also use removeFromSuperlayer and addSublayer: if you prefer. Also, if your search bar isn't the topmost view in your hierarchy, make sure to find out where it is and put it back in the right place using insertSubview:atIndex: or one of the similar methods.
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?