How to achieve spacing between cells/margins? - ios

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

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 force UIImageView of a custom cell to override table separator?

I am having a custom cell where I add a UIImageView as a subview for the cell's contentView.
This UIImageView is placed outside the boundaries of the cell which results with image appearing underneath the table view separator as follows:
I wish that the image will override the table view separator as follows:
How can I do that?
That you can't do with tablview's separator. You first need to hide tableview's separator (i.e set seperator to none from IB or you can set to none by code also).
Take UIView with same width of separator and with height = 1 or 2 if want thick separator. And put it to the end of contentview of cell.
The easiest way is to default separators in tableview and add a simple uiview line as a subview to your cell below your image view.

Position UITableViewCell

Good evening,
I would like to position my custom tableview cell the same as the next picture:
Currently i've got this:
How can i add a margin on the tableviewcell? I've already tried to different things with separatorInsets and contentInsets, but i can't get the desired result.
Does anyone have a suggestion?
Thanks in advance!
You could put all your content inside a UIView, instead of directly adding them to the contentView. Then you add some margin from this viewto the contentView (from your screenshot, should be left, right, top and bottom).
An easy way to embed all your view in a parent view, would be the following: Select all your subviews and go to Editor -> Embed In -> View
Make sure you set the background color for the contentView of the cell to clearColor.
This is how the storyboard should look like (you have more than just one label, of course)
There is no other way, using tableViews to have the cells smaller, in width, than the entire tableView.
Another option would be to use a collectionView instead. There you can specify both width and height for your cells, so obtaining that layout would be easier.
Let me know if you need more help :)
Implementing custom selection to take the padding into account
For this you will need the following:
You will need to add a UIImageView as subview as your view that has the padding, like this:
As you can see from the screenshot, you will need to set the image and the highlightedImage for the UIImageView. The image will be a simple white image, and the highlightedImage and image with the background color you want. For me my too pictures looked like this:
Then, in your custom UITableViewCell class you do the following:
#interface MyTableViewCell()
#property (weak, nonatomic) IBOutlet UIImageView *highlightImageView;
#end
#implementation MyTableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[self.highlightImageView setHighlighted:selected];
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
// this will change the color on your image
[self.highlightImageView setHighlighted:highlighted];
}
Swift implementation
#IBOutlet weak var highlightedView: UIImageView!
override func setSelected(selected: Bool, animated: Bool) {
highlightedView.highlighted = selected
}
override func setHighlighted(highlighted: Bool, animated: Bool) {
highlightedView.highlighted = highlighted
}
Let me know if you have questions.
You want to use nested views:
View 1: Add a view to the content view of the cell which is pinned to the edges a 0 points. Make the background color grey. or make it transparent and set the table background to grey.
View 2: inside view 1
Pin this to the edges of view 1 with leading and trailing margins to match your grey border. Use top and bottom pins with size equal to half the grey margin between cells.
Make the background colour of this view the color of the left thin vertical color bar on your picture.
CTRL drag this view to create an IBOutlet in your code so you can change the color in the cell on a per cell basis.
View 3: inside view 2.
Make this white and pin it at 0 points on top, bottom and trailing edge to view 2.
Pin the leading edge to the thickness of the coloured vertical bar.
Finally put your text etc in view 3 and lay it out.
This will give you the layout you wish.

center custom UITableViewCell

I have a custom UITableViewCell subclass with width of 300.
Since the cell is 20pt shorter than the table view, When the cells get loaded, its placed on the left most position, is there a way to center this custom cell to the tableview's center?
I tried cell.center = self.tableView.center in cellForRowAtIndexPath, but it wont work. any idea?
You can pad your custom cell with 10px to the left so it will only appear to be centered, so you just make the width 310 and move everything else inside by 10 px.
a) Resize your UITableView to 300 and center it.
or
b) Make your cell's width same as UITableView's width. Make your custom cell's background color clearColor. Add one more view to your custom cell subclass that will contain every other view and add every view in that view. Think of this like an illusion where the cell is actually wider but it's contents are centered in a fake view.

How to prevent UITableViewCell backgroundView from stretching

I'm attempting to add a background image to a UITableViewCell so that each cell can have some spacing at the bottom.
I'm setting the cell's backgroundView property like this:
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell"]]];
The "cell" image is 20px taller than the actual heigh of the UITableViewCell, this is on purpose so that my cells can have some spacing at the bottom. Only problem is, the "cell" image will get stretched all the way down the cell's contentView.
Is there a way for me to prevent the cell from stretching the backgroundView all the way down?
Thanks in advance!
Look into the contentMode property of UIView. If the height of your background image is smaller than the height of the cell and you want the image to "stick to the top" try setting
cell.backgroundView.contentMode = UIViewContentModeTop;
Look at the "Content Modes" section on this page for more information. It mentions...
"By default, the contentMode property for most views is set to UIViewContentModeScaleToFill, which causes the view’s contents to be scaled to fit the new frame size."
Two options:
1) Make your image bigger manually, filling it with transparent space in the gaps. (Easier solution).
2) Add another UIView inside your UITableViewCell, make it as big as the image. Add your text label (if any) inside this view.

Resources