SendSubviewToBack on UITableView not working properly in iOS 11

I have a UITableViewController to which I have successfully applied gradient backgrounds in the past by sending the newly added subview back:

//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; 

However, this no longer works in iOS 11, and bgView is actually placed on top of all cells.

enter image description here

Does anyone know how I can fix this? Or maybe I did it wrong all the time?

+5
source share
4 answers

If your cells are transparent, you can try self.tableView.backgroundView = bgView;

+1
source

Another way to fix this is to call [self.view sendSubviewToBack:bgView]; in tableView:willDisplayCell:forRowAtIndexPath:

It works for opaque cells.

0
source

it looks like addSubview (UIView) sendSubview (toBack: UIView) no longer works for UITableViewControllers in iOS11. Therefore, I will change this:

 // 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 
0
source

If you do not want the background view to scroll along with the table view, you can use

 self.tableView.backgroundView = bgView; 

If you need to scroll the background image, change the zPosition layer to a negative value to make it work in iOS 11:

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

Source: https://habr.com/ru/post/1272089/


All Articles