代码之家  ›  专栏  ›  技术社区  ›  Aaron Brethorst

UITableView的顶部和底部有不同的背景颜色

  •  36
  • Aaron Brethorst  · 技术社区  · 17 年前

    如果你在iPhone OS 3.0的邮件应用程序中查看收件箱,你会看到向下滑动会在UISearchBar上方显示灰色背景。

    现在,如果您向下滚动到表格底部,您将看到该端的背景颜色是白色。

    有人知道这个问题的“最佳实践”解决方案是什么吗?谢谢。

    13 回复  |  直到 17 年前
        1
  •  41
  •   Olof    8 年前

    Light gray background in “bounce area”...

    我在哪里发现了这个代码狙击手(稍作修改),它工作得很好:

    CGRect frame = self.tableView.bounds;
    frame.origin.y = -frame.size.height;
    UIView* grayView = [[UIView alloc] initWithFrame:frame];
    grayView.backgroundColor = [UIColor grayColor];
    [self.tableView addSubview:grayView];
    [grayView release];
    

    var frame = self.tableView.bounds
    frame.origin.y = -frame.size.height
    let grayView = UIView(frame: frame)
    grayView.backgroundColor = .gray
    self.tableView.addSubview(grayView)
    
        2
  •  16
  •   Alessandro Francucci    6 年前

    Swift 5.0+

    带扩展的解决方案:

    extension UITableView {
    
        func addTopBounceAreaView(color: UIColor = .white) {
            var frame = UIScreen.main.bounds
            frame.origin.y = -frame.size.height
    
            let view = UIView(frame: frame)
            view.backgroundColor = color
    
            self.addSubview(view)
        }
    }
    

    tableView.addTopBounceAreaView()

        3
  •  14
  •   danbretl    15 年前

    1. 将搜索栏视图放入容器视图中。将表视图的标题视图设置为此容器视图(而不是搜索栏视图本身,这可能是您之前所做的)。

        4
  •  13
  •   Alex Pretzlav    17 年前

    将tableFooterView设置为高度和宽度均为0的视图,该视图在其边界之外绘制。一个简单的方法是向其中添加一个大子视图:

    self.tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    UIView *bigFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)];
    bigFooterView.backgroundColor = [UIColor whiteColor];
    bigFooterView.opaque = YES;
    [self.tableView.tableFooterView addSubview:bigFooterView];
    [bigFooterView release];
    

    相应地调整[UIColor whiteColor]和bigFooterView的宽度(如果你的tableView可以水平移动,你会希望它比320宽)。这样,在顶部,您将看到您的表视图背景是什么,在底部,您可以看到您将此视图的背景设置为什么。

        5
  •  8
  •   Aaron Brethorst    17 年前

    - (void) scrollViewDidScroll: (UIScrollView *) sv
    {
        float percent =  sv.contentOffset.y / sv.contentSize.height;
        percent = 0.5 + (MAX(MIN(1.0f, percent), 0.0f) / 2.0f);
    
        sv.backgroundColor = [UIColor colorWithRed:percent * 0.20392
                                             green:percent * 0.19607
                                              blue:percent * 0.61176 alpha: 1.0f];
    }
    

    - (void)scrollViewDidScroll:(UIScrollView *)sv
    {
            UIColor *backgroundColor = nil;
    
            float percent = sv.contentOffset.y / sv.contentSize.height;
            percent = 0.5 + (MAX(MIN(1.0f, percent), 0.0f) / 2.0f);
    
            if (0.5f == percent)
            {
                backgroundColor = RGBCOLOR(233.0f, 235.0f, 237.0f);
            }
            else
            {
                CGFloat r = 233.0f * (1.0f - percent) + 255.0f * percent;
                CGFloat g = 235.0f * (1.0f - percent) + 255.0f * percent;
                CGFloat b = 237.0f * (1.0f - percent) + 255.0f * percent;
                backgroundColor = RGBCOLOR(r,g,b);
            }           
            sv.backgroundColor = backgroundColor;
    }
    
        6
  •  8
  •   Ivan Smetanin    9 年前

    var frame = self.tableView.bounds
    frame.origin.y = -frame.size.height
    let view = UIView(frame: frame)
    view.backgroundColor = .gray
    self.tableView.addSubview(view)
    
        7
  •  3
  •   lemnar    17 年前

    这可能不是“最佳实践”,但如果你真的想像苹果那样做,有一个私人 UITableView 物业名称 tableHeaderBackgroundColor 灰色是 #e2e7ed .

    你可以把这样的东西放在 -viewDidLoad a方法 UITableViewController :

    UIColor *grayishColor = [UIColor colorWithRed:226/255.0
                                            green:231/255.0
                                             blue:237/255.0 alpha:1.0];
    [self.tableView setValue:grayishColor forKey:@"tableHeaderBackgroundColor"];
    
        8
  •  2
  •   Nikolay Khramchenko    8 年前

       self.tableView.tableFooterView = UIView();
    
       if let tableFooterView = self.tableView.tableFooterView {
             let bigFooterView = UIView();
             bigFooterView.backgroundColor = UIColor.white;
             bigFooterView.isOpaque = true;
             tableFooterView.addSubview(bigFooterView);
    
             bigFooterView.translatesAutoresizingMaskIntoConstraints = false;
    
             tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .trailing, relatedBy: .equal, toItem: tableFooterView, attribute: .trailing, multiplier: 1, constant: 0));
             tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .leading, relatedBy: .equal, toItem: tableFooterView, attribute: .leading, multiplier: 1, constant: 0));
             tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .top, relatedBy: .equal, toItem: tableFooterView, attribute: .top, multiplier: 1, constant: 0));
             tableFooterView.addConstraint(NSLayoutConstraint(item: bigFooterView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 1000));
       }
    
        9
  •  1
  •   Community Mohan Dere    9 年前

    Light gray background in “bounce area” of a UITableView 底部也是如此。希望这能有所帮助:)

    CGRect topFrame = self.tableView.bounds;
    topFrame.origin.y = -topFrame.size.height;
    UIView* topView = [[UIView alloc] initWithFrame:topFrame];
    topView.backgroundColor = [UIColor grayColor]; // change to any color you want
    [self.tableView addSubview:topView];
    
    CGRect bottomFrame = self.tableView.bounds;
    bottomFrame.origin.y = self.tableView.contentSize.height;
    UIView* bottomView = [[UIView alloc] initWithFrame:bottomFrame];
    bottomView.backgroundColor = [UIColor grayColor]; // change to any color you want
    [self.tableView addSubview:bottomView];
    
        10
  •  0
  •   Alexey Rodionov    8 年前

    这是我的解决方案:

        let topColor = UIColor.blue
        let bottomColor = UIColor.black
    
        self.tableView.backgroundColor = topColor
        self.tableView.tableFooterView = UIView(frame: CGRect.zero)
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 1000))
        footerView.backgroundColor = bottomColor
        self.tableView.tableFooterView?.addSubview(footerView)
    
        11
  •  0
  •   rbaldwin    5 年前

    SwiftUI解决方案

     var body: some View {
            
            NavigationView {
                
                List(data, id: \.self) { data in
                    Text("\(data)")
                }
                .onAppear {
                    let headerView = UIView(frame: CGRect(x: 0, y: -400, width: UIScreen.main.bounds.width, height: 400.0))
                    headerView.backgroundColor = .lightGray
                    UITableView.appearance().addSubview(headerView)
                }
                .navigationBarTitle("Title", displayMode: .inline)
            }
        }
    

    enter image description here


    UIView

    let backgroundView = UIView()
    backgroundView.backgroundColor = .black
    UITableView.appearance().backgroundView = backgroundView
    

    enter image description here

        12
  •  -1
  •   Nathan de Vries    17 年前

    tableHeaderView 和 tableFooterView 的性质 UITableView .

        13
  •  -1
  •   Dutchie432    17 年前

    推荐文章