Get rid of line above UITableView - ios

I have a UITableView (which happens to have a UISearchBar) and can't seem to figure out how to get rid of the white/gray border above it. I need to have seamless black between the UISearchBar and the black above it.
I have tried hiding the UISearchBar to see if that had anything to do with it, but the line still appeared.
Any ideas?

In my case it helped to set:
searchBar.clipsToBounds = YES;

You have to customise the UISearchBar background to match according to your requirements.,take a look at this tutorial.

You should try this, by which you can set the color of cell borders and if you want to change the color of a particular cell's border put it in condition as: if (indexPath.row == yourcell):
tableView.separatorColor = [UIColor blackColor];
Also the above method you have to put in CellForRowAtIndexPath method of table view datasource.
Please notify if it works..

This had to do with the pullToRefresh view I was using (using SensibleTableView) - it had some code in drawRect to draw the line.

Related

Swift 4 backgroundColor bug?

I have a problem with transparency in my Views, especially with UITableView.
Just to make sure. I already have 3 years of experience with swift and this is the first time, I am getting this problem.
So in apple's docs it says the following:
backgroundColor
The view's background color.
Changes to this property can be animated. The default value is nil, which results in a transparent background color."
But when applying clear color to backgroundColor or tableView.backgroundColor = UIColor.clear, the background is still appearing white.
Is this a bug or a feature?
EDIT:
The tableView isn't the problem anymore. It is the cell within the tableView.
Already tried solutions are clearing the background color of all subviews. Nothing changed.
EDIT 29.09.17
Ok, I debugged my application and got this:
UICachedDeviceWhiteColor and cachedColor.
A google searching also didn't help.
Edit 29.09.2017 Later that day ...
The problem occurs, when updating from swift 3 to swift 4.
I made a new project with swift 4, but it actually works fine.
Because it is a problem I have to solve. I will try some things and update this post from time to time.
In UITableViewCell and UICollectionViewCell there is a property view called contentView.
A common mistake is to manipulate the cell directly. Instead try to manipulate the cells content view:
Obj-c
UITableViewCell *cell = ....// get the cell
cell.backgroundColor = ... // BAD !
cell.contentView.backgroundColor = [UIColor redColor] // this is the correct way.
Swift
let cell = ...
cell.contentView.backgroundColor = UIColor.red // make sure its the contentView
also a couple small nuances with colors and opacity:
If you want only the background to have opacity (i.e. less than 100%) , make sure you are not changing the view's opacity (i.e. view.alpha = 0.5 BAD) - change the color's opacity value instead:
i.e.
cell.contentView.backgroundColor = [UIColor colorWithRed:... green:... blue:... alpha:0.5];
Another useful trick is to pause the application and press the "visual debug" button.
Search for your view and check it's attributes in the inspector
Found the problem:
When instantiating a variable which holds the view before displaying it, the background color of this view is white. The debugger calls this UICachedDeviceWhiteColor.
Why would you do that?
If you instantiate the view before it is displayed, you can switch with a menu between different views very fast with "bringSubview(toFront: ...).
How to fix:
To fix this you have to instantiate the view every time you want to show it.
Hope I could help some lost souls with this answer and Apple is going to fix this bug.
Verify that the view behind it is not white, then set the current view's (the one with the transparent background) modalPresentationStyle to overCurrentContext. This will allow views behind to show through the UIColor.clear background color.
If you are setting
tableView.backgroundColor = .red
and if it's an empty TableView then the Red will be visible. I give a headerView and FooterView also to visualise.
Now TableView Contents some data, so to reflect the color you need to set background color to cell.
class UserCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = .yellow
}
....
}
You can check out the original project here.

Why is the background of my table view grey when there are no rows?

Why is the background of my table view grey when there are no rows? I can't see where this is set in storyboard
Even setting the background to white explicitly I still get a grey background
Using the defaults for background also results in this grey background:
this might be due to two reason
a) you haven't set data source and delegate and your main view background color is gray
b) cell background color is gray, set that to clear color
Please add this code viewdidload or viewwillappear method
table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
For me it looks like it is a problem of the UITableViewCells, you should set them to
self.backgroundColor = [UIColor whiteColor]; // or clearColor
then also set the UITableView itself inside the controller to backgroundColor white or grey.
Also the snippet of #Rohitsuvagiya can help to let dissappear the separators if there is no additional UITableViewCell.
Please add line your code in viewdidload or viewillappear
table.backgroundColor=[UIColor clearColor];

Setting image to tableview background

I have added an imageview on top of tableview. I need to set it to the back of tableview without setting it as a background on tableview.
sendSubviewToBack:bgView also does not work. Can someone help?
try like this ,
if objects are in xib ,then take imageview infront of tableview in xib,
(or)
if you are adding in programatically then add imageview before adding the tableview
(or)
table.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#""]];
(or)
[table bringSubviewToFront:yourImageview];
Try this
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"table_bg_image.png"]];
Not getting what exactly you want .
One another way from XIB , just simply add it into the xib behind your table View , and set your tableView's background colour as clearcolor, So you'll be able to see the image behind your tableview.

Restore default UITableViewCell backgroundView

In short, I need to restore the default backgroundView for a UITableViewStyleGrouped cell.
cell.backgroundView = ??;
The long story:
I have a grouped tableview with some cells. For every cell the user needs to select some value or do some input. After that he can proceed to the next ViewController by touching a "next" button.
Anyway, to indicate to the user that he missed something, I display a nice red border around the cell with the wrong/missing user input. I do that by setting the backgroudView of the cell with a custom image like this:
cell.backgroundView = myErrorIndicatingImageView;
The cell now looks like this (screenshot, ignore the stars and label)
So far so good, this works like a charm. But after the user corrects the input I want to remove my red border background image and just show the original background again. The original background looks like this (screenshot):
And this seems to be a problem.
I tried several things
// try by setting the background to nil
cell.backgroundView = nil;
this removes the background completely and I'm lost with a cell without background.
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
this does nothing. The myErrorIndicatingImageView is not visible. Even a [cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView] does not help.
Right now the only solution I have is to create a new UITableViewCell for every single call to cellForRowAtIndexPath. This works but is just bad code and ignores completely the technique to reuse cells.
There must be a simpler solution...something like
cell.backgroundView = [self.tableView getDefaultBackgroundView];
what about trying this:
cell.backgroundView = [[UIView alloc] initWithFrame:cell.backgroundView.frame];
Or you can make the background view as a UIImageView and set 2 images in problem and fixed
Assuming you are doing this with a custom table cell class, save the original backgroundView in some ivar, then apply the new background. When you want to reset the background, set the backgroundView back to the original, saved background view.

Highlight table view cell programmatically

I have a cell in a static table view which I want selected (highlighted blue) when the view appears. Does anyone know of a way to do this?
I have tried using cell.selected = YES;. This will make the cell selected for a very short time (it goes blue, then white just after).
I found the solution. I used cell.selected = YES; in viewDidLoad. Doing it in viewDidAppear will work.
I think you can write cell.selected = true in didSelectRowAtIndexPath also.

Resources