Custom Tableview cell width issue - ios

I'm changing the background colour of the labels dynamically, the width of the cell seems to be static and is working properly in case of 6s only, the background colour fades if the size of the display increases
cell.container.layer.backgroundColor = GetColor().randomColor(indexPath.row).colorWithAlphaComponent(0.5).CGColor
cell.title.layer.backgroundColor = GetColor().randomColor(indexPath.row).colorWithAlphaComponent(0.8).CGColor
New Code// after changing the opacity
// cell.container.layer.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.85).CGColor
cell.title.backgroundColor = GetColor().randomColor(indexPath.row).colorWithAlphaComponent(0.5)

Mohit Arya,
I beilieve you have applied Autolayout constraint on the label inside the cell :) If yes please verify if its the same as I have shown below if not please add autolayout constraints properly,
As you are setting the color of the label itself and want it to cover the whole cell setting all the four constraint to 0 is necessary
EDIT:
Now as per your comment, if constraints are same as I have given below your label must be covering the whole cell :)
Then all you have to do is to
[cell.label setBackgroundColor: GetColor().randomColor(indexPath.row).colorWithAlphaComponent(1.0)]
cell.label.opaque = YES;

Check background color (and background color alpha value) for all views in your cell view hierarchy. For example, issue like this can appear if your cell's contentView have background color with alpha < 1, and your label (which size don't fit contentView) have same background color. Thats all what we can say by information you provided.
So, use different colors, or use alpha = 1, or use clear color in label.

I found the solution..the following code was interfering with the UI, I still don't know why the container.bounds is not returning the actual width
container.layer.shadowColor = UIColor.lightGrayColor().CGColor
container.layer.shadowOpacity = 1
container.layer.shadowOffset = CGSizeMake(0, 1);
container.layer.shadowRadius = 2
container.layer.shadowPath = UIBezierPath(rect: container.bounds).CGPath

Related

Don't use SafeArea inset on iPhone X for first UITableViewCell

I have a UITableView and the first row is used as a sort of header cell with a full bleed background image and other elements. For this cell I do NOT want to use the SafeArea and I want the UIView to expand all the way to the edge of the screen. Currently I get this:
I have tried to set this manually for the cell:
if (#available(iOS 11.0, *)) {
headerCell.insetsLayoutMarginsFromSafeArea = NO;
headerCell.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0);
UIEdgeInsets i = headerCell.layoutMargins;
NSLog(#"Left: %f", i.left);
}
Sadly this doesn't work.
Here is a mockup of what it should look like, with the first cell contents NOT being affected by the safe area:
Is there any way to do what I am wanting just for the first cell?
I found that I needed to uncheck the Content View Insets To Safe Area checkbox that is on the UITableView.
Doing this fixed the issue!
Marking the "Content insets" under Size Inspector to "Never" worked for me.
In code, it could be accomplished by
tableView.insetsContentViewsToSafeArea = false
Just go to the tableview Size inspector and look for the Content Insets by default it will be set to Automatic change that to Never as shown like in the image below

How to set a DropShadow on UITableView itself

I'm trying to set a dropshadow on a UITableView which is being added programmatically.
The frame height is being set to a certain percentage of the screen. So I want to set a dropshadow on the tableview itself.
I tried doing the following:
tableview_results.layer.shadowPath = UIBezierPath(rect: tableview_results.frame).cgPath
tableview_results.layer.shadowColor = UIColor.black.cgColor
tableview_results.layer.opacity = 1
tableview_results.layer.shadowOffset = CGSize.zero
tableview_results.layer.shadowRadius = 10
However this doesn't seem to do anything.
When I try searching for a solution, I only find stuff regarding how to set a dropshadow on the last cell of the UITableView. However this would not work for me since then the shadow will only be displayed when the last cell is displayed.
I need the shadow to be always present on the UITableView.
The shadow path should be set to the tableview_results.bounds not frame. The bounds is the rectangle around the table view in the coordinate system of the table view itself, and that's the correct coordinate system in which to specify the shadow path. Alternatively, you can just not set the shadow path at all and the shadow will draw in the correct place (though perhaps there's a performance benefit to setting the path explicitly if you can do so reliably).
You also need to set the shadowOpacity not the opacity of the layer, to 1. And you need to tell the tableview_results to not clipToBounds. So something like this:
tableview_results.layer.shadowPath = UIBezierPath(rect: tableview_results.bounds).cgPath
tableview_results.layer.shadowColor = UIColor.black.cgColor
tableview_results.layer.shadowOpacity = 1
tableview_results.layer.shadowOffset = CGSize.zero
tableview_results.layer.shadowRadius = 10
tableview_results.clipsToBounds = NO;
And for that last line, if you feel like the code is cleaner by only talking to the layer, you can equivalently use tableview_results.layer.masksToBounds = NO.
Note that there is a side effect of turning off clipping: Now you might see table view cells beyond the bounds of the table view itself! So there might be better ways to get the shadow effect. You could, for example, wrap the table view in a simple superview that tightly bounds the tableview, and give the shadow to that superview. Just a thought.

Rounded prototype corners iOS9.2

How would I change my cells in the UITableViewCells to have rounded corners? Kind of like this:
I like the gaps that are present, so is there away to adjust that as well?
In the ContentView of the UITableViewCell you can set, this lines the get the rounded corner.
self.layer.cornerRadius = 5.0
self.clipsToBounds = true
self.layer.masksToBounds = true
To give the gap, you need to set the default content view to Clear Color (to be transparent), and add a View inside the cell, with a smaller size then the cell.
It will look like this in the storyboard.

How to customise the UITableView's Separator View?

I have a problem of the implementation of UITableViewCell's separator.
As you can see from the screenshot, there is a white gap visible before the separator, I think that is because I set the bg colour of the cell as light grey and also I put the inset of the separator as 53.
My first attempt was instead of using the separator, I was trying to draw the lines at the end of the cell by my self. But since on selection of the row the content of the row get updated, and there is a lot of issue regarding the calculation of the height of the cell.
So basically it is really hard for me to draw the line pixel precise at the end of the cell.
This left me the option to access the cell's separator's view, which currently not aware of any easy way, and fill the gap with my default background colour of the table.
My question is, how can I access the separator view
Or
Do I have any other alternatives to implement what I want?
Thank you very much.
UITableView doesn't provide APIs to access or modify the cells separator view directly. However, it has methods to change it's color, style, etc. Most of the times, though, the only solution for a custom separator is to draw it yourself, or better, to set the cell's backgroundView property to a simple view with a line subview or layer in it (a subview, although has some overhead, gives you the flexibility of autoresizing automatically using autoresizing masks or auto layout).
e.g.
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.bounds];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backgroundView.bounds) - 1, CGRectGetWidth(backgroundView.bounds), 1.0f];
[lineView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];
[backgroundView addSubview:lineView];
cell.backgroundView = backgroundView;
Use this workaround:
Put UIImageView at bottom edge of cell and you can set image as per your requirement
STEP 1: Set divider as per image
STEP 2: Set Separator style to none
Since iOS 7 you can use this
cell.separatorInset = UIEdgeInsetsZero;

UICollectionViewCell pixel artifacts

I have a custom UICollectionViewCell, I have few views in its contentView.
I have an UIImageView with image (1 pixel colored) and I use it to fill my view.
I also cut the corners like this -
self.myBackground.layer.cornerRadius = 8.0;
self.myBackground.layer.masksToBounds = YES;
Color of my view and all collectionViewCells and their contentViews is [UIColor whiteColor]
But I am getting weird grey line below it. I am not setting it anywhere and I don't need it.
How can I remove it?
I have found the answer, it is separator. After setting separator view to nil it disappeared.

Resources