Change UIBarButtonIcon in an if and else statement in Swift - iphone

Inside of an If and else statement I want to change the custom image of my BarButtonItem. How can I Do these by default I already have one custom Image but know I want that under some circumstances it changes to another custom Image.

If you have your images in Assets.xcassets you can do this:
if something {
yourBarButtonItem.image = imagename
} else {
yourBarButtonItem.image = anotherimagename
}
that should work. Else, if you have your image just in a folder inside your project you can to this:
if something {
yourBarButtonItem.image = UIImage(named: "imagename.extension")
} else {
yourBarButtonItem.image = UIImage(named: "imagename.extension")
}

Related

Can I put a static image in a UIButton.imageView? that is animating?

I have a custom UIButton (MovesButton) with an image that I don't want to change or remove. But when I set button.imageView?.animationImages with bunch of images and start animating it, it removes my button's initial photo I have. If I have the static photo as a background, it looks like it is on fire. I want the fire to be as a background and preferably in the same UIButton class.
private func putAnimation(button: MovesButton) {
var images: [UIImage] = []
if button.animation == ButtonAnimations.None {
print("None11")
return
} else if button.animation == ButtonAnimations.SmallFire {
for i in 0 ... ButtonAnimations.SmallFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.SmallFire.0)\(i)")!) //ButtonAnimations.SmallFire.0 = "smallFire"
}
} else if button.animation == ButtonAnimations.BigFire {
for i in 0 ... ButtonAnimations.BigFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.BigFire.0)\(i)")!)
}
} else { print("weird button animations") }
button.imageView?.animationImages = images
button.imageView?.animationDuration = 1
button.imageView?.startAnimating()
}
I don't want to use a hacky trick of putting an imageView or another button behind MovesButton
I want the same animation result as a button.imageView.animationImages but as the backgroundImage. Is this possible?
Instead of image view animation, use image animation. Call UIImage animatedImage(with:duration:) to form an animated image. Set that as your button’s background image and you’re all set.

Default Image Isn't Displaying When Setting Image

I am trying to set the default avatar image to a "ninja" image. Default image isn't showing however with this code. Not sure what is wrong.
ProfileService.showOtherUser(user: bet.sentToUser) { [weak self] (profile2) in
self?.profile2 = profile2
if (profile2?.imageURL == "") {
cell.userImage.image = UIImage(named: "ninja")
}else{
let imageURL = URL(string: ((profile2?.imageURL ?? "")))
cell.userImage.kf.setImage(with: imageURL)
}
}
}
Put your image in storyboard to your image view this way till you not change it programmatically it will remain the default one

How can I UITest the change of the image of a UIButton in Swift?

When I tap a UIButton, the image should change to reflect its new state (e.g. Record -> Pause etc).
In my XCode UITest function, how do I interrogate the buttons current image after the tap to assert that its image has changed correctly to the correct image's .png file?
I did it like this
// Find the button by the image name
// In this example the image's names are "record_image" and "pause_image"
// "_" are replaced by " " in the image name when searching
let recordButton = XCUIApplication().buttons["record image"]
recordButton.tap()
XCTAssertFalse(recordButton.exists) // Record button won't exist anymore since the image has changed
let pauseButton = XCUIApplication().buttons["pause image"]
XCTAssertTrue(pauseButton.exists)
pauseButton.tap()
XCTAssertFalse(pauseButton.exists)
XCTAssertTrue(recordButton.exists)
I did like this, not best way but efficient for me,
every image change I changed accessibility identifier then checked the access.ids
public func setFollowed(_ isFollowed: Bool) {
if isFollowed {
followButton.setImage(UIImage(named: "followed-green-icon"), for: .normal)
followButton.accessibilityIdentifier = "ProfileInfoView_followButton_followed"
}
else {
followButton.setImage(UIImage(named: "follow-blue-icon"), for: .normal)
followButton.accessibilityIdentifier = "ProfileInfoView_followButton_follow"
}
}
sample UI test part:
func getFollowButton(isFollowed: Bool) -> XCUIElement {
if isFollowed == true {
return app.descendants(matching: .button)["ProfileInfoView_followButton_followed"]
} else {
return app.descendants(matching: .button)["ProfileInfoView_followButton_follow"]
}
}
then tested returned element, state changed etc.

How to access Image Assets like an array in Swift

In my app I'm allowing the user the change some of the UI elements based on color. So, I have three versions of an image which can be used for a button and I'd like to select the image programmatically:
PSEUDO CODE
"image0", "image1", "image3"
var userChoice:integer
myButton.setImage("myImage"+userChoice , .normal)
I've seen this solution in SO:
Programmatically access image assets
What would be the Swift equivalent code?
Right now I'm using image literal:
self.But_Settings.setImage(#imageLiteral(resourceName: "settingswhite"), for: UIControlState.normal)
but of course Xcode changes this part "#imageLiteral(resourceName: "settingswhite")" to an icon which cannot be edited.
Then don't use image literal. Image literals are just that - a hard value in code that you can't change during run time. Load an image dynamically from your bundle:
if let image = UIImage(named: "myImage" + userChoice) {
self.But_Settings.setImage(image, for: .normal)
}
Do you think this would help? : But_Settings.setImage(UIImage(named: "play.png"), for: UIControlState.normal). Here you are using name of the asset
Since it's a limited number of choices it sounds like a good place for an enum.
enum ImageChoice: Int {
case zero = 0, one, two
var image: UIImage {
switch self {
case .zero:
return // Some Image, can use the icon image xcode provides now
case .one:
return //another image
case .two:
return //another image
}
}
}
Then you can easily get the correct image by initializing the enum by using an Int value.
guard let userChoice = ImageChoice(rawValue: someInt) else { return //Default Image }
let image = userChoice.image

Remove uiimageview from view

I have a feed and i am wanting to display images if the user does not have an image, it will say null in the json. if the user does have a image, the image will be set with the image url.
i want to remove it so it will not take up space in the tableview cell.
my code is:
if imageStringFromJson == nil {
//how do i remove the imageview? <-- need help here
} else {
//set image from url in imageview
}
Try:
imageView.removeFromSuperview()
Or:
imageView.hidden = true
Or animate:
UIView.animateWithDuration(1) {
imageView.alpha = 0
}
To animate back to showing, try this:
UIView.animateWithDuration(1) {
imageView.alpha = 1
}