Positioning images and that are unhidden - ios

I'm working on a project to have hidden images appear when the button is pressed. My only problem is that when I click on the button the image appears. I then have to click on the button again for it to show up in the correct area of my view controller. Can someone help point me in the right direction please?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.hidden = true
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.hidden = false
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
}

This may or may not help, but i would recommend that instead of using the image.hidden property, set the alpha value of the image. So image.alpha = 0 for hidden and image.alpha = 1 for visible. This would also allow you to then fade the image in and out if you wanted. Additionally I would set the image in viewDidLoad() instead of the Action. Here's how I would redo your code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.alpha = 0
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.alpha = 1
}
}
I also believe setting the frame twice isn't useful as the frame will be set in viewDidLoad()

Related

How can I create a button that makes new Object as previous one in Swift?

I want to create a button that makes a new image. But this new image should be the same as the first one (bild).
I also tried to see what is working with print command.
The console shows all the prints but nothing more happens when I click the button. The new image is not appearing in the app.
I don't have any more ideas about that problem.
#IBOutlet weak var bild: UIImageView!
#IBAction func NewImage_btn(_ sender: Any) {
let newImage = UIImageView(frame: bild.frame)
print("0")
let newY = bild.frame.origin.y + 40.0
print("1")
newImage.frame.origin.y = newY
print("2")
self.view.addSubview(newImage)
print("3")
}
At first you need to set an image to it for see it, second you don't need to add it to view again: so here is working:
#IBOutlet weak var bild: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func NewImage_btn(_ sender: Any) {
let newImage = UIImageView(frame: CGRect(x: bild.frame.origin.x, y: bild.frame.origin.y + 40, width: bild.frame.width, height: bild.frame.height))
newImage.image = UIImage(named:"Your image name")
}
I think the missing line is
newImage.image = bild.image

Swift class with uibutton.addTarget to UIView not working

I created a new file with the following class:
import Foundation
import UIKit
var Feld = classFeld()
class classFeld {
let button = UIButton()
func createButton() -> UIButton {
button.frame = CGRect(x: 10, y: 50, width: 200, height: 100)
button.backgroundColor=UIColor.black
button.addTarget(self, action: #selector(ButtonPressed(sender:)), for: .touchUpInside)
return button
}
#objc func ButtonPressed(sender: UIButton!) {
button.backgroundColor=UIColor.red
}
}
And this is my ViewController:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
mainview.addSubview(Feld.createButton())
self.view.addSubview(mainview)
}
var mainview=UIView()
}
When I start the app a black button is created but when I click on it it doesnt color red.
If I add the button instead of
mainview.addSubview(Feld.createButton())
to
self.view.addSubview(Feld.createButton())
it works and the button turns red.
May I anyone explain me why? It should make no difference if I add something to self.view or to a subview which is then added to self.view?
Because you need to give it a frame and add it to self.view
var mainview = UIView()
This is because you are just initializing a UIView without giving it any frame and adding it to the main view.
You need to give frame to your mainview also. For example :
class ViewController: UIViewController {
var mainview = UIView()
override func viewDidLoad() {
super.viewDidLoad()
mainview.frame = CGRect(x: 10, y: 50, width: 300, height: 300)
self.view.addSubview(mainview)
mainview.addSubview(Feld.createButton())
}
}
Here is changes on ViewController class and working fine:
class ViewController: UIViewController {
var mainview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
mainview = UIView(frame: self.view.bounds)
mainview.addSubview(Feld.createButton())
self.view.addSubview(mainview)
}
}

Change background image with button click

I can change background image on viewDidLoad func like this:
let myBackgroundImage = UIImageView (frame: UIScreen.main.bounds)
myBackgroundImage.image = UIImage(named: "wallpaper-1-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(myBackgroundImage, at: 0)
i would like to do same action with button click like this:
#IBAction func flip4btn5(_ sender: Any)
{
let myBackgroundImage = UIImageView (frame: UIScreen.main.bounds)
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(myBackgroundImage, at: 0)
}
but it does not change background image. Why ? What do you think ?
I'm using Swift 4.1.
Don't create a new image. just change the picture of the first UIImage in your button action like this:
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
Try this code, It's working fine.
class ViewController: UIViewController {
var myBackgroundImage = UIImageView (frame: UIScreen.main.bounds);
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myBackgroundImage.image = UIImage(named: "wallpaper-1-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(myBackgroundImage, at: 0)
}
#IBAction func flip4btn5(_ sender: UIButton) {
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
}
}
Don't keep adding UIImageViews everytime you want to change the background image.
Place a UIImageView in the view hierarchy where it is needed and keep a reference to it. Then set the image property on it as needed.
class ViewController: UIViewController {
#IBOutlet weak var backgroundImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
backgroundImageView.image = UIImage(named: "wallpaper-1-iphone-8-plus.png")
}
#IBAction func flip4btn5(_ sender: Any) {
backgroundImageView.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
}
}
You can set the contentMode either in the storyboard or in viewDidLoad. You don't need to keep setting it after updating an image.
let myBackgroundImage = UIImageView (frame: CGRect.zero)
override func viewDidLoad() {
super.viewDidLoad()
myBackgroundImage.image = UIImage(named: "wallpaper-1-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.addSubview(myBackgroundImage)
}
#IBAction func ButtonPressed(_ sender: Any) {
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
}

IOS Swift3 how to Remove Custom UIView randomly

I am building an iOS Application in swift 3, where I am creating dynamic UIViews. I need to remove custom view randomly. Please help me I am stuck with this for a long time. Thanks In Advance
class ViewController: UIViewController {
var myView: subView!
var y : CGFloat!
#IBOutlet weak var addButton: UIButton!
override func viewDidLoad() {
y = 1
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func cancelbutton(_ sender: UIButton) {
myView.removeFromSuperview()
}
#IBAction func buttonAction(_ sender: Any) {
y = y + 110
myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))
myView.backgroundColor = UIColor.green
myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
self.view.addSubview(myView)
}
}
As you can see in the above image when i click on Close the SubView(custome View) closes, but where as MyView with green color does not go and stays there. Someone please Help.........
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
Bundle.main.loadNibNamed("subView",owner: self, options:nil)
self.addSubview(self.views)
Bundle.main.loadNibNamed("subView",owner: self, options:nil)
self.addSubview(self.views)
}
override init(frame: CGRect)
{
super.init(frame: frame)
Bundle.main.loadNibNamed("subView", owner: self, options: nil)
views.frame = bounds
self.addSubview(self.views)
}
#IBAction func buttonAction(_ sender: Any) {
views.removeFromSuperview()
}
The thing you are doing wrong is that you add multiple views of subView that why you selected does not remove. Please modify your code like given below.
The thing I have done is that whenever you will add new subView you will also set its tag value and when you select view to remove its tag value and place an if statement on that tag value.
class ViewController: UIViewController {
var myView: subView!
var y : CGFloat!
var tag : Int = 0
#IBOutlet weak var addButton: UIButton!
override func viewDidLoad() {
y = 1
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func cancelbutton(_ sender: UIButton)
{
let selectViewTagValue : Int = sender.tag /// save the selected view tag value
for object in self.view.subviews {
if ((object is subView) && object.tag == selectViewTagValue)
{
object.removeFromSuperview()
}
}
}
#IBAction func buttonAction(_ sender: Any) {
y = y + 110
myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))
myView.tag = tag
myView.actionButton.tag = tag
tag = tag + 1
myView.backgroundColor = UIColor.green
myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
self.view.addSubview(myView)
}
Try to change cancel action like this:
func cancelbutton(_ sender: UIButton)
{
if let myView = sender.superview {
myView.removeFromSuperview()
}
}
Just remove the button's container view, not the global myView.
I managed to fixed the delete issue, but currently I am unable to relocate the positions the customs views, as shows in the picture below, Kindly help me with this.

how to change uiimage's image on button click in swift?

I'm trying change an uiimage control's containing image by clicking a button in swift. For this, i wrote a simple a "guess how many fingers i'm holding up" app. I've read some stackoverflow articles but couldn't solve the problem. Below you can see my code and my ui. How can i change change image on click?
Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var myImageView: UIImageView!
#IBOutlet var inputField: UITextField!
#IBAction func clickedGuessButtonAction(sender: AnyObject) {
println("Guess button clicked")
var randomX = Int(arc4random()%6)
println("randomX = \(randomX)")
var guess = inputField.text.toInt();
let image0 = UIImage(named: "images/qm.jpg")
let image1 = UIImage(named: "images/tick.jpg")
let image2 = UIImage(named: "images/cross.jpg")
if((inputField.text) != nil){
if(guess == randomX){
println("correct")
myImageView.image=UIImage(named: "tick.jpg") // THIS IS NOT WORKING
myImageView.image=image1 // THIS IS NOT WORKING TOO
inputField.resignFirstResponder();// hides keyboard
}
else
{
println("wrong")
myImageView.image=UIImage(named: "cross.jpg")
inputField.resignFirstResponder();//hides keyboard
}
}
else{
println("invalid input. requires integer only")
inputField.resignFirstResponder();// hides keyboard
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And here's my UI:
You don't need to specify either the asset catalog's name or the image file's extension. Try removing those. For example,
myImageView.image = UIImage(named: "tick")
It may depend on the version of Xcode you are using. For me the following worked:
ImageView.image = UIImage(named: "Elephants.jpg")
In Swift:
myImageView = "nameOfImage";

Resources