简体   繁体   中英

How do I get a translucent view to float over my table view?

I'm trying to make a summary box at the top of the screen that remains there while below it you have a table view. When you scroll the tableview I'd like it to be visible but blurry through the summary box, rather like the status bar along the top.

So I've created a view with two subviews - one for the summary and one for the tableview, which works. I've set the summary view to be transparent and you can see the table view behind it. Unfortunately, the top cell in the table view is obscured.

So my question is - can I add a vertical offset so the top cell starts below the summary box?

Alternatively, is there a better way to achieve what I want - a box that stays at the top of the table as you scroll up and down, and is transparent/translucent?

Set the top content inset of the table view to the height of the floating view:

UIEdgeInsets insets = UIEdgeInsetsZero;
insets.top = floatingView.frame.size.height;
tableView.contentInset = insets;

UITableView inherits contentInset property from UIScrollView .

I can't really tell what you're asking for, but I think you want a transparent fixed header.

Rob Mayoff's way should work- Adding a subview to the tableview, and setting the tableview's contentInsets to the height of that subview should what I think you want. But I'll suggest an alternative- using the section headers built in to the UITableView control.

Try this- (a very basic implementation of a fixed section header for a tableview with one section)

(make sure your UITableView's delegate and dataSource are set to your view controller)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:(CGRect){0,0,320,100}];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.6;
    return view;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM