sendSubviewToBack上的UITableView未在iOS中正常工作11

问题描述:

我有一个的UITableViewController,而我在过去的成功应用渐变背景,通过发送新添加的子视图来支持:sendSubviewToBack上的UITableView未在iOS中正常工作11

//performed on viewDidLoad 
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.5*280, 1.5*SCREEN_HEIGHT)]; 
bgView.backgroundColor = [UIColor yellowColor]; 
CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = bgView.bounds; 
gradient.startPoint = CGPointMake(0, 0); 
gradient.endPoint = CGPointMake(1, 1); 
UIColor *topColor = UIColorFromRGB(0x229f80); 
UIColor *bottomColor = UIColorFromRGB(0x621ad9); 
gradient.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil]; 
[bgView.layer insertSublayer:gradient atIndex:0]; 
[self.view addSubview:bgView]; 
[self.view sendSubviewToBack:bgView]; 
bgView = nil; 

然而,这不再作品在iOS 11中,bgView实际上被放置在所有单元格的顶部。

enter image description here

任何人都知道我怎样才能解决这个问题? 或者我可能一直在做错吗?

+0

[self.view sendSubviewToBack:bgView];而不是这个,试试这个方法 - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 – Ellen

如果你的细胞是透明的,那么你可以尝试self.tableView.backgroundView = bgView;

另一种方式来解决它是调用[self.view sendSubviewToBack:bgView];tableView:willDisplayCell:forRowAtIndexPath:

它适用于没有透明细胞也。

+0

但是这将尝试发送子视图每次显示一个单元格时,我认为这是多余的。 – CristiC

+0

如果每次显示单元格时都不执行此操作,则每个较新的单元格(由cellForRow返回)都将位于此视图下。 –

看来addSubview(UIView)sendSubview(toBack:UIView)不再适用于iOS11中的UITableViewControllers。所以我换到这一点:

// in ViewDidLoad 
// set the backgroundImage 
    let backgroundImage = UIImageView(frame: self.view.bounds) 
    backgroundImage.image = UIImage(named: "background.png") 
//  self.view.addSubview(backgroundImage) // NO LONGER WORKS 
//  self.view.sendSubview(toBack: backgroundImage) // NO LONGER WORKS 
    self.tableView.backgroundView = backgroundImage 

如果不需要与表视图一起滚动背景视图,可以使用

self.tableView.backgroundView = bgView; 

如果您需要背景视图是滚动,变层的zPosition为负值,使其在IOS 11工作:

[self.view insertSubview:bgView atIndex:0]; 
bgView.userInteractionEnabled = NO; 
bgView.layer.zPosition = -1;