且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有加载在设备上的背景图像

更新时间:2023-02-22 09:30:27

尝试下面在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....