本文介绍了没有加载在设备上的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码将图像放在背景中并应用模糊效果:
I have this code that places the image in the background and applies a blur effect:
let effect = UIBlurEffect(style: .Dark)
override func viewDidLoad() {
let backgroundView = UIView(frame: view.bounds)
backgroundView.autoresizingMask = resizingMask
backgroundView.addSubview(self.buildImageView())
backgroundView.addSubview(self.buildBlurView())
tableView.backgroundColor = UIColor.clearColor()
tableView.backgroundView = backgroundView
tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
}
func buildImageView() -> UIImageView {
let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
imageView.frame = view.bounds
imageView.autoresizingMask = resizingMask
return imageView
}
func buildBlurView() -> UIVisualEffectView {
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds
blurView.autoresizingMask = resizingMask
return blurView
}
当我在模拟器中运行它时,它加载正常。但是当我在iPhone 6上进行测试时,它只会加载黑色背景。
When I run this in the simulator it loads fine. But when I test it on an iPhone 6 it just loads a black background.
任何有关如何解决此问题的帮助将不胜感激。
Any help on how to solve this problem would be appreciated.
推荐答案
尝试下面在Xcode 8中测试过的代码。代码测试为UIView ..
Try below code tested in Xcode 8 it worked. Code tested as a UIView..
**Answer 1:** From your code
// Change bLurView Style to .dark or .extraLight If you want
let effect = UIBlurEffect(style: .light)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let backgroundView = UIView(frame: view.bounds)
// backgroundView.autoresizingMask = resizingMask
view.addSubview(self.buildImageView())
view.addSubview(self.buildBlurView())
/////Code tested in UIView not in tableView.It won't do any difference just let you know///////
tableView.backgroundColor = UIColor.clearColor()
// tableView.backgroundView = backgroundView
tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
/////////////////////////////////////////////////////////////////////
}
func buildImageView() -> UIImageView {
let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
imageView.frame = view.bounds
// imageView.autoresizingMask = resizingMask
return imageView
}
func buildBlurView() -> UIVisualEffectView {
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds
// blurView.autoresizingMask = resizingMask
return blurView
}
答案2:
override func viewDidLoad() {
super.viewDidLoad()
// Add a background view to the table view
let backgroundImage = UIImage(named: "your image file")
let imageView = UIImageView(image: backgroundImage)
self.tableView.backgroundView = imageView
imageView.contentMode = .ScaleAspectFit
// no lines where there aren't cells
tableView.tableFooterView = UIView(frame: CGRectZero)
//set background colour to light color or clear color to get a transparent look
tableView.backgroundColor = .lightGrayColor()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = imageView.bounds
imageView.addSubview(blurView)
}
If we want to make the table view cells totally transparent you can just set their background color to clear:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor(white: 1, alpha: 0.5) // UIcolor.clear
}
以上代码的输出如下....
output of the above code as below....
这篇关于没有加载在设备上的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!