Add "shadow" to view - ios

I have cells with 2 "rectangles". What i want is, add shadow to right rectangle. It will be better explained on screenshot:
From left is first part of cell (first rectangle) and on a right in second part. I want to add a shadow that look like on a screenshot. I tried:
-(void)addInnerShadow{
self.bgDetailsView.layer.shadowColor = [UIColor colorWithHexString:#"#a4c2e0"].CGColor;
self.bgDetailsView.layer.shadowOffset = (CGSize){SHADOW_SIDE_HEIGHT,0};
// self.vSelectionBack.layer.shadowRadius = 1.4;
self.bgDetailsView.layer.shadowRadius = SHADOW_SIDE_HEIGHT;
self.bgDetailsView.layer.shadowOpacity = .5;
}
Where bgDetailsView is second (right) view, but it has no effect.

Ok, so as I understand you want to add shadow to right view and shadow casts over the left view.
To do so:
You can set self.bgDetailsView.clipsToBounds = NO, it will make shadow visible but will also make your shadow to be visible over and below the right view.
You can put right view in container. Set clipsToBounds = NO in right view and make container view a bit bigger to the left.
You can also make a container view with additional CALayer or UIImageView with shadow image. Put your right view to right of this additional view/layer and set in container clipsToBounds = NO.
You can also use shadowPath property of right view layer and put it in container view:
self.bgDetailsView.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowRect].CGPath;

Related

How to add shadow to uicollection view ? ios

I need to add a bottom shadow my collection view,here is my code:
weakself!.layer.shadowPath = UIBezierPath(rect: weakself!.bounds).CGPath
weakself!.layer.masksToBounds = false
weakself!.layer.shadowOffset = CGSizeMake(0.0, 3.0);
weakself!.layer.shadowColor = UIColor.blackColor().CGColor
weakself!.layer.shadowOpacity = 0.5
the above code is work perfect with other views. but it make the collection view to scroll above it's bounds. as you can see here in the bellow picture where the collection view scroll above the search bar:
If you need to do this you should add the collection view to a plain view, don't change the collection view, add the shadow to the plain view (the collections views superview).
I'd really recommend not using a shadow but instead presenting the collection as a popover or at least with a full screen backing view which dimmed the background and made the collection view more obvious (and prevented taps on other things like the partially visible buttons).
you are using swift.But I'm well in objective c.so the concept is same.then try this.
self.youView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.youView.layer.shadowOffset = CGSizeMake(1, 1);
self.youView.layer.shadowOpacity = 1;
self.youView.layer.shadowRadius = 1.0;

Draw border "within" UIViewController

I need to draw a visible border that goes around the edge of the screen within my view controller. I tried setting the view's borderWidth and borderColor, but nothing appeared on the screen. How would I accomplish this?
I tried the following code and it worked.
self.view.layer.borderColor = UIColor.orangeColor().CGColor
self.view.layer.borderWidth = 3

Show view/point/smt under clipped area

Is it possible to show something under clipped area?
For example I have:
UIView *viewA = ...
viewA.layer.cornerRadius = radius;
viewA.layer.masksToBounds = YES;
So, here I have view with rounded corners. Then I want to add another viewB as subview of viewA and present it under clipped area of viewA. Help me, please, how can I do that?
You mean that you want viewB to be clipped too or that it is shown in the clipped area?
If it is the former:
viewA.clipsToBounds = YES;
UIView *viewB = ...
viewB.frame = ... // something relative to viewA.bounds
[viewA addSubview:viewB];
EDIT:
Then for what you want to do you have to set viewA.clipsToBounds = YES, create a container view for both viewA and viewB and put them both as subviews of this container. You can't clip a superview and have an unclipped subview.

UIView border cover sub-view?

I have a UIView which includes a UIButton which is partially on UIView. I have a problem when I draw a border on my UIView. Please have a look at my screenshot:
You can see the border is above the UIButton, why? Can anybody suggest? Thanks
Thanks for aăâ, I found a solution.
Basically the border is always drawn on top of everything
What I did is:
Create a UIView with color of border
Create another UIView as the child the main UIView which is a little bit smaller than the first one. The color of this newly create UIView is the main color
Here is the code:
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
self.backView.layer.cornerRadius = 15;
self.backView.layer.masksToBounds = YES;
The result is:
It's more or less what I need although it's not perfect.
It could have to do with the order that the objects are drawn. In your storyboard's "Document Outline", views that are lower down in a view controller's outline are drawn later. Perhaps the button is not the last drawn view, like you want?

How to force an element in a view to be non-transparent when parent view has alpha set to translucent

I have a view that is slightly translucent, I set the alpha to about .75 and it contains a button as a sub element.
I want the button to be completely opaque and I set the opaque property in IB but still the button appears as translucent.
Any pointers?
Thanks!
A subview of a superview is allways minimum as transparent as its subview.
Subview alpha = 0.5 and superview alpha = 0.1 would result in an alpha of the subview of 0.05.
The only way of achieving that is changing the view hierarchy. Your subview must not be a subview any more. It may still be at the same position. (May require different position value).
An example.
Your background view is backgroundView
Your superview is firstView
Your subview is overlayView
Your current hierarchy is
backgroundView -> firstView -> overlayView
You should change that to:
backgroundView -> first View
\-> overlay View.
If your firstView's position is (10, 10) and your overlayView's position was (20,20) then change your overlayView's position to (30,30) because it now is within the coordinate system of background view.
For your code:
At some point you used to have
[backgroundView addSubView:firstView];
[fisrtView addSubView:overlayView];
change that to
[backgroundView addSubView:firstView];
[backgroundView addSubView:overlayView];
If you did not code that but defined it within IB then just move overlayView within the views tree accordingly so that it is on the same level as firstView.
Regardless whether you code it or draw it in IB, make sure that firstView comes first and overlayView comes next. Otherwise firstView could hide overlayView when its alpa is greater than 0.

Resources