Scrungepipes
Scrungepipes

Reputation: 37581

When a table view is dragged down, how to set the color of the view behind it?

I have a green navigation bar and a green table view header that together look like this:

enter image description here

But when the user scrolls the table view down it appears like this:

enter image description here

I'm attempting to set the color of the grey portion that is revealed when the user drags down to also be green, but can't work out how to do so.

If I set the tableview's background color to green this does not affect it, setting that to green sets the view behind the grey strip but does not affect that strip itself:

enter image description here

Here is the grey strip in the view hierarcy:

enter image description here

Is there some way I can set it to green, so when the user drags down they don't see they gray beneath?

The table view in the storyboard is like this:

enter image description here

Upvotes: 1

Views: 319

Answers (3)

Guillaume Ramey
Guillaume Ramey

Reputation: 263

Dobiho's answer in Swift:

var frame = tableView.bounds
frame.origin.y = -frame.size.height
let bgView = UIView(frame: frame)
bgView.backgroundColor = UIColor.green
bgView.autoresizingMask = .flexibleWidth

tableView.addSubview(bgView)

Upvotes: 0

dobiho
dobiho

Reputation: 1035

I had a same problem, I solved it with this code.

    CGRect frame = self.ContactTable.bounds;
    frame.origin.y = -frame.size.height;
    UIView* bgView = [[UIView alloc] initWithFrame:frame];
    bgView.backgroundColor = [UIColor systemBackgroundColor];
    bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // Adding the view 
    [self.tableView insertSubview:bgView atIndex:0];

Upvotes: 1

par
par

Reputation: 17724

Use the backgroundView property of UITableView:

let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.green
tableView.backgroundView = backgroundView

It will automatically resize to the size of the table view.

Upvotes: 0

Related Questions