Swift 3: Image does not show when setting isHidden=false

余生长醉 提交于 2019-12-12 04:37:53

问题


I followed a tutorial about how to create an iOS app where a title image and a background image are shown once a button is hit (there are checked as "hidden" in the attribute inspector). I am using Swift 3 and Xcode 8.

That's what the code looks like:

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var background: UIImageView!
  @IBOutlet weak var titleImage: UIImageView!

  @IBOutlet weak var welcomeBtn: UIButton!

  @IBAction func welcomePressed(_ sender: UIButton) {
    background.isHidden = false
    titleImage.isHidden = false
    welcomeBtn.isHidden = true
  }

  //some more inherited functions....

The title image is shown and the welcome button disappears as expected but the background does not appear.

If I uncheck the box "hidden" in the attribute inspector and do "background.isHidden = true" instead, everything gets black once the button is pressed.

I don't really understand this behaviour. Is this a bug or a feature? Shouldn't the background show when isHidden is set to false? Shouldn't the background image disappear, showing everything else, in the second scenario?

In the connections inspector everything seems to be correct, but I don't really get why I can't delete the connection "view - Background":

Outlets: background - Background, titleImage - Title Image, view - Background, welcomeBtn - Welcome Btn

Related Actions: welcomePressed - Welcome Btn Touch Up Inside

When I unhide both images in the attribute inspector they will be shown correctly when starting the app.


回答1:


As Rob suggested I tried the view debugger and found that the background UIImageView never appears.

I'm not sure why but for the background UIImageView I could not see a referencing outlet in the connection inspector. When I right-clicked on the word "Background" under "View Controller Scene" -> "View Controller" -> "View" a black outlet window opened and the connection was there! So I deleted that connection in that black window and the ViewController and control-dragged another IBOutlet from the background UIImageView to the ViewController.

Now it works! The background UIImageView also appears in the view debugger.

So the original question is now: How can a connection disappear in the connections inspector but still be there when you right-click on it and that black outlet window opens?



来源:https://stackoverflow.com/questions/39821504/swift-3-image-does-not-show-when-setting-ishidden-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!