So I have been out of the iOS coding world for a bit and I am going through Swift at the moment.
In Objective-C I was able to change an image on the fly, but to do so I had to set the image delegate. The other reason I set the delegate was so I could hide the UIImageView
In Swift I seem to have an issue with this.
With the playground I can do this fine:
let responseImageView:UIImageView!
responseImageView.hidden = true
But in the project this does not work.
Can someone help me out here?
In my project I have this code :
#IBOutle var responseImageView: UIImageView!
//Inside a function I have this:
let smile = "smile.png"
imageName = UIImage(named: smile)!
responseImageView = UIImageView(image: imageName)
That code does not work.
And I can't hide the image by doing this:
responseImageView.hidden = true
If you created the actual UIImageView in the Storyboard or Interface Builder (and connected the IBOutlet correctly - it's always a good idea to double check this if something isn't working as expected!) you should not try to instantiate it again in code just to change the image. Instead, change the image by accessing the UIImageView's .image property, like so:
let smile = "smile.png"
let image = UIImage(named: smile)
responseImageView.image = image
Edit:
Just to clarify: by "instantiate" I specifically mean this line from you project code:
responseImageView = UIImageView(image: imageName)
Doing this in your function will break the connection you've set up in the storyboard. If you put a break point after that line or a println("\(responseImageView.superview)") you'll see that your instance of responseImageView is now no longer attached to a superview (explaining why you can change it's image or hide or unhide it all you want - you'll never see any change in the app).
Related
I'm trying to learn how to use autolayout programmatically in Swift using this tutorial. I added the PNG to the Assets folder, but when I try to add an UIImageView in the ViewController
let imageView = UIImageView(image: bear)
I get the error "use of unresolved identifier 'bear'".
However, if I go to the Main.storyboard, I can add the image fine. Any tips on why this image isn't recognized in the ViewController? (I tried this with multiple PNGs in case it was a bad image, but same result).
bear needs to be a type UIImage, try something like this
func addImage() {
let imageView = UIImageView()
let bear = UIImage(named: "bear") // whatever the name of that image file is, within your assets
imageView.image = bear // setting your bear image here
}
I came across the below Swift code snipped in Firebase documentation here:
// Reference to an image file in Firebase Storage
let reference = storageRef.child("images/stars.jpg")
// UIImageView in your ViewController
let imageView: UIImageView = self.imageView
// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")
// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
In this particular example, why did they need to assign self.imageView to a new constant and then use that new imageView instead? Why not using the main self.imageView?
Thank you!
The only reason I can imagine is that they wanted to make it clear that an image view exists. There is absolutely no reason to reassign the image view, especially considering that they have the same exact name.
I've got this code
override public func viewDidLoad() {
let imageBlankCard = UIImage(named: "BlankCard.png")
let imageViewBlankCard = UIImageView(image: imageBlankCard)
rockCard.addSubview(imageViewBlankCard)
scissorCard.addSubview(imageViewBlankCard)
paperCard.addSubview(imageViewBlankCard)
self.view.addSubview(rockCard)
self.view.addSubview(scissorCard)
self.view.addSubview(paperCard)
}
But when I run the playground, only the latest UIView (paperCard) is displayed properly, the other cards are not shown at all as you can see in the image here: there should be two cards like the last one, in place of the arrows.
Can someone help me? Thanks everybody!
According to the documentation,
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
This explains the behaviour that you're getting.
One solution to this is to create three separate image views and add each of them as a subview.
Judging by the variable names, I think you are creating something similar to a rock paper scissors game. And you have three views that each holds an image view with the image of a blank card. And you want to add the image of a stone, paper and scissors as an overlay to the image view. If that's what you want to do, why not just make rockCard, paperCard and scissorsCard themselves UIImageViews and set the image to be BalnkCard.png?
let imageBlankCard = UIImage(named: "BlankCard.png")
rockCard.image = imageBlankCard
scissorsCard.image = imageBlankCard
paperCard.image = imageBlankCard
I found a bunch of links on SO that show how to set the navigation bar to an image in Objective-C, but none in swift. The one I did find, didn't help: UINavigationBar setBackgroundImage in AppDelegate with Swift
In the didFinishLaunchingWithOptions in the AppDelegate I'm attempting to do this with swift with no avail:
let headerImage = UIImage(named: "header.png")
UINavigationBar.appearance().setBackgroundImage(headerImage, forBarMetrics:.Default)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
var navigationVC:UINavigationController = UINavigationController(rootViewController: tableVC)
let frame = UIScreen.mainScreen().bounds
window = UIWindow(frame: frame)
window!.rootViewController = navigationVC
window!.makeKeyAndVisible()
How can I set the navigation bar to an image in swift?
Thanks in advance.
You are using a retina image but Xcode does not know that because it does not have a file name that contains #2x.
So if you rename your image to header#2x.png it works.
You might want to consider using an Asset Catalog for your images to avoid things like this.
How can I display image inside UIImageview? Then change images according to pressed button.
I am very new please go easy.
To do this, first make sure you have the image available in the viewcontroller (by creating an association between the image on your storyboard, and in your viewcontroller).
Let's pretend it's called imageView.
Programmatically, you can say:
imageView.image = UIImage(named: ("nameofasset"))
What I actually ended up doing is creating a static function responsible for determining which image to return as such:
class func GetImage(someValueWhichDrivesLogic: Int)->UIImage
{
if (someValueWhichDrivesLogic == 1)
{
assetName = "imageone" //note you don't add .png, you just give it the same name you have in your standard images folder in XCode6
}
else if (someValueWhichDrivesLogic == 2)
{
assetName = "imagetwo" //again, this is the actual name of my image
}
//Return an image constructed from what existed in my images folder based on logic above
return UIImage(named: (assetName))
}
So now that the function above has been created, you could just do something like this:
imageView.image = GetImage(1)
imageView.image = GetImage(2)
This would actually assign the images dynamically to the image. You could just do this, or if you're literally doing something as simple as putting it into a button click you can just take the easier approach I specified above.
Thanks let me know if any questions!