【问题标题】:Unable to set background image for UICollectionView无法为 UICollectionView 设置背景图片
【发布时间】:2015-09-22 22:19:28
【问题描述】:
我一直在尝试将自定义背景图片添加到我的应用中。
我使用了这里介绍的方法:
How to fill background image of an UIView
这些代码在正常的 UIViewController 和 UITableViewController 中都成功,但在 UICollectionViewController 中失败。就是一片漆黑。
据我了解,UITableViewController 的结构与 UICollectionViewController 类似。我比较了这两个控制器的设置,它们都是相同的(在视图部分)。为什么代码在其中一个上有效,而在另一个上无效?
【问题讨论】:
标签:
ios
objective-c
swift
uicollectionview
uibackgroundcolor
【解决方案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()
}
【解决方案2】:
这将比背景颜色完美。
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
}
【解决方案3】:
实现这一点的最佳方法是通过 CollectionView 的 viewDidLoad 中的代码实现
let bgImage = UIImageView();
bgImage.image = UIImage(named: "bg4");
bgImage.contentMode = .ScaleToFill
self.collectionView?.backgroundView = bgImage
【解决方案4】:
// 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];
【解决方案5】:
它正在处理 uicollectionview
self.collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "image.png")!)
【解决方案6】:
我的一个朋友给了我解决方案。
诀窍是将集合视图背景设置为清除而不是默认设置。
我已将所有视图设置为默认值,所以我认为它在集合视图中应该相同。
但是为什么只有在集合视图中默认背景是黑色而在其他视图中默认是清晰的?这让我很困惑。
【解决方案7】:
如果您将背景颜色分配给 self.view,请尝试将其分配给 self.collectionView。