Failed to set background image for UICollectionView
I am trying to add a custom background image to my application. I have used the methods given here: How to fill a UIView background image
These codes succeeded in both normal UIViewController and UITableViewController, but failed in UICollectionViewController. It's just black.
As I understand it, the UITableViewController is similar to the structure of the UICollectionViewController. I compared the setting for these two controllers and they are all the same (in the view section). Why does the code work on one but not the other?
source to share
My friend gave me a solution.
The trick is to set the background of the collection to clear, not the default. I set all views to default, so I thought it should be the same in the collection view.
But what about the default background black in the collection view, and the default in others? It confuses me.
source to share
// Create a Programmatically UICollectionview in iOS And Set a UICollectionView Background Image
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tiger"]]];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.view addSubview:_collectionView];
source to share
try setting background image for UICollectionView Swift 4 or later
1. Add this extension to your code from the main class
extension UICollectionView {
func setEmptyMessage(_ message: String,_ img:UIImage) {
let image = UIImageView()
image.contentMode = .scaleAspectFit
image.image = img
let messageLabel = UILabel()
messageLabel.text = message
messageLabel.font = UIFont.boldSystemFont(ofSize: 20)
messageLabel.textColor = .gray
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.sizeToFit()
let mainView = UIView()
mainView.addSubview(image)
mainView.addSubview(messageLabel)
//Auto Layout
image.translatesAutoresizingMaskIntoConstraints = false
image.centerXAnchor.constraint(equalTo: mainView.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: mainView.centerYAnchor , constant: -40).isActive = true
messageLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.topAnchor.constraint(equalTo: image.bottomAnchor, constant: 20).isActive = true
messageLabel.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 10).isActive = true
messageLabel.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: 10).isActive = true
self.backgroundView = mainView
}
func restoreBackgroundView() {
self.backgroundView = nil
}
}
2. How to use
if Data.count == 0 {
self.collectionView.setEmptyMessage("set the message", UIImage(named: "imageName"))
} else {
self.collectionView.restoreBackgroundView()
}
source to share