Search code examples
iosswiftxib

IOS: Image from assets folder not/shown in device OR simulator


I'm experiencing some troubles on one of my projects.
Shortly I've made a project using storyboards and now I'm using xibs (plus some programmatic views) and I'm also using Xcode 10 + macOS mojave (with the new update). Thing is I have an image that I have to put as a header on a view (unfortunately this view is a hamburger/side menu view or however you want to call it). I made everything to work, but the thing is I didn't want to rely on the IB and just drop an image and underneath a table view.
Firstly I put there the image view and it was ok on device, but on simulator you couldn't see that image, saying that the "menuImage" (how it was named) couldn't be found in the nib "com.comp.comp" which was weird because the image was in the assets.
Then I just removed that despicable view and made the table view cover the whole view. I created programatically a headerView and an imageView and added the imageView as a subview to the headerView then I set the tableView.headerView to be = to that headerView I made, with an image inside. On simulator it was filling the whole view with that image, which made and still makes no sense, and on device you could only see a blank view. If I was setting that headerView I created the backgroundColor to .red I was able to see the red header inside the table view. I could set that height to be 10 or 200 and it was perfect, but when I wanted to put the image it was just white. I tried setting the tableView's, headerView's and imageView's backgrounds to .clear, but didn't work.
Even dropping an image view inside the table view within the IB didn't work. Now I'm using the first solution, the image view on top of the table view.
Now I'm gonna finish this up (sorry :'( )... I did delete the app and restarted the iPhone and repeat this "solution" several times, also did create a new project and put manually the images I needed in the assets.
I really looked up on stack overflow but didn't find anyone to experience the same issue as me, so not any solution to it.
I'm sorry for the long post; I hope you guys have at least an idea or hint or anything.
EDIT: As I said about the code snippet...here it is:

private func setupHeaderView() {
        let image = UIImage(named: "menuImage")
        let imageView = UIImageView(image: image)
        imageView.contentMode = .scaleAspectFill

        tableView.translatesAutoresizingMaskIntoConstraints = false

        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 150))

        headerView.addSubview(imageView)

        imageView.topAnchor.constraint(equalTo: headerView.topAnchor).isActive = true
        imageView.bottomAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true
        imageView.leftAnchor.constraint(equalTo: headerView.leftAnchor).isActive = true
        imageView.rightAnchor.constraint(equalTo: headerView.rightAnchor).isActive = true

        tableView.tableHeaderView = headerView
    }  

Even by commenting translatesAutoresizingMask and those 4 lines with the anchor constraints, I wasn't able to see the image. Now I have an imageView right on top of the table view (did this in the xib file) and I'm not able to see the image on the device, but on the simulator I can.


Solution

  • Your post is difficult to comprehend as no code was posted nor screenshots if you used storyboard, i.e. both leading to complications in your description. Here is what can be done.

    An image from image assets is generally used by an

    var imageView = UIImageView(frame: yourFrame)
    imageView.image = UIImage(named: "nameFromAssets")
    imageView.contentMode = .scaleAspectFit //This helps keep the image original aspect ratio
    self.view.addSubview(imageView) // Or whatever view you are using
    

    Or by using Storyboard

    1) Place an imageView on the screen somewhere and organize it as you'd like (constraints, etc)

    2) In the Inspector, choose the image you want for it using the dropdown

    3) Provide the contentMode you'd like