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?

+6


source to share


7 replies


The best way to implement this is through code inside your viewDidLoad for the CollectionView



    let bgImage = UIImageView();
    bgImage.image = UIImage(named: "bg4");
    bgImage.contentMode = .ScaleToFill


    self.collectionView?.backgroundView = bgImage

      

+9


source


He is working on uicollectionview



self.collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)

      

+4


source


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.

+1


source


If you are assigning background color to self.view then try assigning it to self.collectionView.

0


source


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

      

0


source


This will work better than the background color.

- (void)viewDidLoad
    {
     [super viewDidLoad];    
    self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
    }

      

0


source


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()
}

      

0


source







All Articles