7

我一直在尝试将自定义背景图像添加到我的应用程序中。我使用了这里介绍的方法: How to fill background image of an UIView

这些代码在普通 UIViewController 和 UITableViewController 中都成功,但在 UICollectionViewController 中失败。它只是全黑。

据我了解,UITableViewController 的结构与 UICollectionViewController 相似。我比较了这两个控制器的设置,它们都是相同的(在视图部分)。为什么代码在一个上工作,而在另一个上却不行?

4

7 回答 7

10

实现这一点的最佳方法是通过 CollectionView 的 viewDidLoad 中的代码来实现

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


    self.collectionView?.backgroundView = bgImage
于 2016-05-11T15:40:24.167 回答
4

它正在开发 uicollectionview

self.collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)
于 2015-07-22T15:35:03.777 回答
1

我的一个朋友给了我解决方案。

诀窍是将集合视图背景设置为清除而不是默认值。我已将所有视图设置为默认值,所以我认为它在集合视图中应该相同。

但是为什么只有在集合视图中默认背景是黑色而在其他视图中默认是清晰的?这让我很困惑。

于 2015-07-05T20:22:08.940 回答
1

试试这个为 UICollectionView Swift 4 或更高版本设置背景图像

1.在主类之外的代码中添加这个扩展

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. 如何使用

if Data.count == 0 {
  self.collectionView.setEmptyMessage("set the message", UIImage(named: "imageName"))
} else {
  self.collectionView.restoreBackgroundView()
}
于 2019-07-26T04:53:26.103 回答
0

如果您将背景颜色分配给 self.view,请尝试将其分配给 self.collectionView。

于 2015-07-05T20:14:26.880 回答
0
// 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];
于 2016-02-19T06:56:03.637 回答
0

这将比背景颜色完美。

- (void)viewDidLoad
    {
     [super viewDidLoad];    
    self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
    }
于 2018-05-17T09:39:47.357 回答