【问题标题】:Background image not loading on device背景图像未在设备上加载
【发布时间】:2017-02-19 22:43:31
【问题描述】:

我有这段代码将图像放在背景中并应用模糊效果:

 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 上测试它时,它只会加载黑色背景。

任何有关如何解决此问题的帮助将不胜感激。

【问题讨论】:

  • 你的背景看起来是阴影黑色还是漆黑...
  • 答案更新了看看...

标签: ios iphone swift uiimage


【解决方案1】:

试试下面在 Xcode 8 中测试的代码,它可以工作。作为 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
   }

以上代码的输出如下......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 2016-08-04
    • 2014-06-28
    • 2014-02-17
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多