UITableView 上的 sendSubviewToBack 在 iOS 11 中无法正常工作
sendSubviewToBack on UITableView not working properly in iOS 11
我有一个 UITableViewController,我在过去通过将新添加的子视图发送到后面成功地应用了渐变背景:
//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;
但是,这在 iOS11 中不再有效,bgView 实际上位于所有单元格的顶部。
有人知道我该如何解决这个问题吗?
或者也许我一直都做错了?
如果你的细胞是透明的那么你可以试试self.tableView.backgroundView = bgView;
另一种修复方法是在 tableView:willDisplayCell:forRowAtIndexPath:
中调用 [self.view sendSubviewToBack:bgView];
它也适用于不透明的单元格。
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
如果不需要背景视图与table视图一起滚动,可以使用
self.tableView.backgroundView = bgView;
如果您需要滚动背景视图,请将层的 zPosition
更改为负值以使其在 iOS 11:
中工作
[self.view insertSubview:bgView atIndex:0];
bgView.userInteractionEnabled = NO;
bgView.layer.zPosition = -1;
我有一个 UITableViewController,我在过去通过将新添加的子视图发送到后面成功地应用了渐变背景:
//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;
但是,这在 iOS11 中不再有效,bgView 实际上位于所有单元格的顶部。
有人知道我该如何解决这个问题吗? 或者也许我一直都做错了?
如果你的细胞是透明的那么你可以试试self.tableView.backgroundView = bgView;
另一种修复方法是在 tableView:willDisplayCell:forRowAtIndexPath:
[self.view sendSubviewToBack:bgView];
它也适用于不透明的单元格。
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
如果不需要背景视图与table视图一起滚动,可以使用
self.tableView.backgroundView = bgView;
如果您需要滚动背景视图,请将层的 zPosition
更改为负值以使其在 iOS 11:
[self.view insertSubview:bgView atIndex:0];
bgView.userInteractionEnabled = NO;
bgView.layer.zPosition = -1;