Light gray background in “bounce area” of a UITableView

前端 未结 16 1909
一整个雨季
一整个雨季 2020-11-28 23:05

Apple\'s iPhone apps such as Music and Contants use a search bar in a UITableView. When you scroll down so that the search bar moves down, the empty space above the scroll v

相关标签:
16条回答
  • 2020-11-28 23:10

    In my case the solution was to create a headerview for the table and assign a color, it solved the black background in bounce area in my apps when in dark mode. I did the same to its tableFooterView.

     table.tableHeaderView = UIView()
     table.tableHeaderView!.backgroundColor = UIColor.white
        
    
    0 讨论(0)
  • 2020-11-28 23:11

    This code works in Swift fot UITableView:

    var frame = self.tableView.bounds
    frame.origin.y = -frame.size.height
    frame.size.height = frame.size.height
    frame.size.width = UIScreen.mainScreen().bounds.size.width
    let blueView = UIView(frame: frame)
    blueView.backgroundColor = UIColor.headerBlueColor()
    self.tableView.addSubview(blueView)
    
    0 讨论(0)
  • 2020-11-28 23:11

    I just encountered this issue myself and found a solution.

    Cause

    I used Spark Inspector to examine the layout of the table view - which really helped.

    Thing is, that in this scenario the UITableView has 3 subviews:

    1. UITableViewWrapperView
    2. UIView - With backgroundColor set to light gray color
    3. UISearchBar

    While you swipe the tableview content downwards, the second subview height is dynamically increasing to fill the space between the UITableViewWrapperView and the UITableView frame origin.

    Solution

    Setting the backgroundColor or backgroundView property won't effect the 2nd subview. What you need to do is find the second view and change its color, like so:

    if (_tableView.subviews.count > 1) {
        _tableView.subviews[1].backgroundColor = THE_TARGET_COLOR;
    }
    

    In my case I needed all views to be white so I used the following which is less prone to future changes of UITableView view hierarchy by Apple:

    for (UIView *subview in _tableView.subviews) {
        subview.backgroundColor = [UIColor whiteColor];
    }
    
    0 讨论(0)
  • 2020-11-28 23:14

    I don't think you want to override drawRect. Most likely what you're seeing is the background colour of another view or the window, which lies "behind" (i.e. is a superview of) the table view. There's usually a fairly complex layers of UIViews in Apple's UI widgets. Explore the view hierarchy in GDB, look at [myView superview] and then [someSuperView subviews] and try manipulating their BG colours in the debugger to see if you can find which one it is. However, if you implement a fix this way, be warned that it may not be future compatible.

    You might also try setting the BG colour of one of the views behind the tableview in Interface Builder (or of the window itself).

    0 讨论(0)
  • 2020-11-28 23:18

    To extend on HusseinB's suggestion:

    Swift 3

    let bgView = UIView()
    bgView.backgroundColor = UIColor.white
    self.tableView.backgroundView = bgView
    

    Objective C

    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.tableView setBackgroundView:bgView];
    
    0 讨论(0)
  • 2020-11-28 23:20

    I will give you the best way to do this.
    First set the background color of the table view to the one you want in interface builder.
    Then respond to the UITableView delegate tableView:willDisplayCell:ForIndexPath: method like this

    - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCelll*)cell forIndexPath:(NSINdexPath*)indexPath
    {
         [cell setBackgroundColor:[UIColor whiteColor]];
    }
    


    Another Method is :
    in ViewDidLoad method (or anywhere you like) set the tableView background color to clear color like this:

    self.tableView.backgroundColor = [UIColor clearColor];
    

    and then set the superview color to white

    self.tableView.superview.backgroundColor = [UIColor whiteColor];
    
    0 讨论(0)
提交回复
热议问题