Why adding sublayer overlaps subviews? - ios

For example I have a view with UILabel as subview. If at some time I add sublayer with some background color to this view's layer, then this label will disappear. Can someone explain this behavior?

When you add your UILabel as a subview of your view it is adding your UILabel's layer as a sublayer of your view's layer. So when you add another sublayer to your view's layer it will be on top of your UILabel's layer.
You can either add your background layer before you add the UILabel or do:
Swift
view.layer.insertSublayer(backgroundLayer, below: yourLabel.layer)
Objective C
[view.layer insertSublayer:backgroundLayer below:yourLabel.layer]
and it should put the background behind the label.

What worked for me was to insert the layer at a specific index like this:
view.layer.insertSublayer(backgroundLayer, at: 0)

Related

How to achieve label embededd in UIImageView in iOS Swift like shown in image?

I want to achieve label/Button embededd in UIImageView in iOS Swift like shown in image.Has anyone done that?
You just need to play with UIView, UILabel And UIImageView.
You view hierarchy will be like below...
- UIView (mainView)
- UIImageView (imageView)
- UILabel
You need to make mainView.clipsToBounds = true so that after giving corner radius to it, it's subview will not go beyond its superview's bounds.
For imageView, you can set its content mode to aspect fill/ aspect fit as per your requirement.
Have a square UIView with clipsToBounds property set to YES which contains UIImageView and UILabel. The image view should cover all the view frame. The label should be positioned at the bottom of the view over the image view. Then just set appropriate corner radius (half of the view's height) for the view's layer and set its masksToBounds property to YES. That's it

How to achieve spacing between cells/margins?

How can I customize my UITableViewCell to get this appearance ?
I don't want to use a custom UIView for my UITableViewCell?
You will need to create a UITableViewCell subclass for this.
The spacing between the cells isn't really spacing. It is fake.
Each cell has a shadow image that has some of the background around it.
You'll need to add a UIView that then has a shadow around it and is inset from the edge of the actual cell. Then add the contents of the cell to that.
Add this inside UITableViewCell subclass :
override func layoutSubviews() {
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10))
}
Create a subclass of UITableViewCell.
Add a View and place required UIElements to its contentView.
Make UIEgdeInset to how much you want.
Custom view color should be lighter than the cell then it will be look like what you want.
It's all about how you set up your UITableViewCell's XIB really.
Add a UIView as a child view of the contentView in the UITableViewCell. Set the trailing, leading, top and bottom spacing of this UIView to it's parent view (contentView) to 5 or whatever value you desire. Set the background colour of the contentView as Clear Colour. Set the backgroundColour of your UITableView to the desired colour with the background colour of the contentView being white.
You can set the cornerRadius of the layer of the UIView to 5.0 to achieve the cornered effect as in your image

Swift: remove layer, without removing UIView in UITableViewCell

I have UITableViewCell with UIView in it.
I made some CABasicAnimation and attach it to new CAShapeLayer, then I add this layer to my super layer in my UITableViewCell:
self.layer.addSublayer(myLayer!)
All nice except that myLayer (and his animation) showing above my UIView.
I want that label be below UIView.
I achieve this by adding my UIView layer the same way:
self.layer.addSublayer(myViewLayer!)
In this case, my UIView layer be on the top of the CAShapeLayer with animation.
But I have a problem, I need to remove layer of UIView - myViewLayer because it violates width of the UIView when scroll.
When animation is done, and I need to remove layers, I can remove CAShapeLayer - myLayer without troubles.
myLayer?.removeFromSuperlayer()
But when I try to do the same with myViewLayer, my UIView removed too. But I don't want that, I need my UIView on screen and in my view hierarchy.
I don't understand why, if look to self.layer.sublayers I see this (before adding layers):
[<CALayer: 0x7fd1f0d4fe40>, <CALayer: 0x7fd1f0ddc950>]
And after animation done and myLayer is removed:
[<CALayer: 0x7fd1f0d4fe40>, <CALayer: 0x7fd1f0ddc950>, <CAGradientLayer: 0x7fd1f0de3660>]
As you can see CAGradientLayer is a layer of my UIView. So, I haven't it before I manually add it to sublayers array. How I can remove it, without removing my UIView?
OR how can I add myLayer below UIView?
EDIT
In few words I have this layer hierarchy before animation:
[<CALayer: 0x7fd1f0d4fe40> <- (I think this is UITableViewCell layer), <CALayer: 0x7fd1f0ddc950> <- (then, this is UITableViewCell content view layer)]
In the same time, I have this view hierarchy:
UITableViewCell -> Content View -> UIView
I need to add new layer with animation, below UIView (I want that UIView partially cover that new layer with animation). How I can do this?
I haven't my UIView layer in the UITableView layer hierarchy, so I can't just add layer with animation using addLayer:below: and so on.
I found the problem. I made a simple mistake. I used self.layer.sublayers, but I must use self.contentView.layer.sublayers this is my mistake.
When I found that, I was able to fix another issues and use addLayer:below:.
So, if you don't see a layer that must be in sublayer of your object, means that you're looking at the wrong place, think where it can be else and you'll find it, like in UITableView you need to work with contentView.
You can add myLayer below UIView.layer by setting
myLayer.zPosition = -1
The example below could have fixed your problem as well.
First solution:
self.avPlayerLayer.zPosition = 0 // or -1
self.view.layer.addSublayer(self.avPlayerLayer)
Or second solution
self.view.layer.insertSublayer(self.avPlayerLayer, below: self.button1.layer)

Adding a static background to a uicollectionview

Is it possible to add a static background to a collectionview because at the moment the background scrolls with the cells and the image looks quite bad.
my code at the moment
collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg.png"]];
There are 2 ways to do what you're asking.
add the background to the UICollectionView as a subview like you're doing now and implement the scrollViewDidScroll: delegate method. Inside that method, set the frame of the background view based on the contentOffset of the collection view which will make it appear to be static on the screen. You will just need to set the frame's Y origin to be the contentOffset.y and it should work. (If there is a non-zero contentInset you may need to do a bit of additional math to get it right.)
add the background to the superview of your collection view, underneath the collection view. This is an easier and probably more efficient solution since you don't need to mess with the contentOffset at all since the background will not be in the scroll view itself.
if I understand your requirement correctly, here's how you can do it:
1) Add your static image as a subview to the parentView.
2) Set the backgroundColor of collectionView to [UIColor clearColor]
3) Add collection view as a subview to the parentView.

How to set a border for UITableView?

I created a view which consists of one UITableView and one UIScrollView, now I need to draw a border for the UITableView so that it can be isolated from the UIScrollView.
Check out UIView layer property which allows you to define visible border for the view. Try out the following:
#import <QuartzCore/QuartzCore.h>
...
self.yourtableview.layer.borderWidth = 2;
self.yourtableview.layer.borderColor = [[UIColor whiteColor] CGColor];
Also remember to include QuartzCore.framework in your project Frameworks list.
There is another way to set the table boarder.
1.You just add the UIView whose size is little larger than table view.
Add the table view as a subview of the view.
As, if table view frame size is (2,2,100, 200), then the view size should be (0,0,104,204).
2.Set the backgroundColor of the view as you want to set for the border color.

Resources