Button Coming From Bottom After Pressing - ios

I'm making an extremely simple iOS app. I press a button and the image changes. The problem I'm running into is every time I press the button, it's like the button goes to the bottom of the phone simulator and lays on top of the button I originally pressed.
Also the image only stays for a split second before disappearing. (I'm assuming these actions go hand in hand).
Here is the code if anyone is able to help.
import UIKit
class ViewController: UIViewController {
#IBAction func generateHero(_ sender: UIButton) {
//list of Images in array
let image : NSArray = [ UIImage(named: "batman.jpg")!,
UIImage(named: "the-flash.jpg")!,
UIImage(named: "Deadpool.jpg")!,
UIImage(named: "green-arrow.jpg")!,
UIImage(named: "iron-man.jpg")!]
//random image generating method
let imagerange: UInt32 = UInt32(image.count)
let randomimage = Int(arc4random_uniform(imagerange))
let generatedimage: AnyObject = image.object(at: randomimage) as AnyObject
self.heroImage.image = generatedimage as? UIImage
}
#IBOutlet weak var heroImage: UIImageView!
}

You are doing a lot of unnecessary conversions in random image generation, if you have used Array instead of NSArray you could use .randomElement() function that returns random element of array. So you would just do
self.heroImage.image = image.randomElement()

Related

How do I add an identifier to the UIImageView?

I have an array with separate UIImages and I am displaying them using an UIImageView, how do I add an identifier to them so that I can switch to the view controller with the specific data related to that image.
Below is the code:
class Orders2ViewController: UIViewController, OrdersBaseCoordinated {
var orderList: [OrderInfo] = [OrderInfo( itemImage: UIImage(named: "printer.jpeg")!, itemSKU: 12567), OrderInfo(itemImage: UIImage(named: "ipad.jpeg")!, itemSKU: 34521), OrderInfo( itemImage: UIImage(named: "hoodie.jpeg")!, itemSKU: 93620)]
lazy var someImageView: UIImageView = {
let theImageView = UIImageView()
let tap = UITapGestureRecognizer(target: self, action: #selector(Orders2ViewController.tappedMe))
theImageView.addGestureRecognizer(tap)
theImageView.isUserInteractionEnabled = true
theImageView.image = orderList[selectedIndex].itemImage
theImageView.translatesAutoresizingMaskIntoConstraints = false
return theImageView
}()
#objc func tappedMe(sender: UITapGestureRecognizer)
{
coordinator?.passImageData(j: )
}
I want to pass an identifier in my tappedMe function to recognize which image was tapped on, I did find other answers on SO that mentioned gesture.view.tag but I don't want to create another view, rather navigate to the next controller with an identifier. Is there a way to do this?
Since UIImageView inherits from UIView, you can use it’s parameter called tag. In your example you can set theImageView.tag = orderedList[selectedIndex].itemSKU. Then each time tap was recognized, you can just use this itemSKU to do move to the next screen.
However it seems that you have only one UIImageView on the screen and you use selectedIndex to determine which image you need. So you can just do like that:
#objc func tappedMe(sender: UITapGestureRecognizer) {
coordinator?.passImageData(j: orderList[selectedIndex].itemImage)
}
I passed the itemImage just to show you how it can be done. You can use whatever you need.

How to print by press of a button a simple quote in swift at random from an array?

I am completely new to coding.
I started using this app called MIMO and learned a few bits and pieces. In one chapter they let us code a simple "dice app" where by pressing a button a number from 1 - 6 appears. Now I wanted to rewrite that so that at the button press the app displays a quote from a predetermined array.
I am completely stuck, however.
Here's what I got:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var quotesLabel: UILabel!
let quotes = ["Quote1!", "Quote2!"]
let randomIndex = Int(arc4random_uniform(UInt32(quotes.count)))
let randomQuote = quotes[randomIndex]
print(array[randomIndex])
override func viewDidLoad() {
super.viewDidLoad()
}
}
Basically any arbitrary code must be run in a method, in this case in an IBAction which is triggered when the button is pressed. The method viewDidLoad is not needed.
Change the code to
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var quotesLabel: UILabel!
let quotes = ["Quote1!", "Quote2!", "Quote3!", "Quote4!"]
#IBAction func showRandomQuote(_ sender : UIButton) {
let randomIndex = Int(arc4random_uniform(UInt32(quotes.count)))
let randomQuote = quotes[randomIndex]
quotesLabel.text = randomQuote
}
}
In Interface Builder drag a button into the canvas of the view controller
Connect (⌃-drag) the button to the IBAction and the label to the IBOutlet
Run the app and press the button

How do I make my ImageView scrollable without affecting the rest of the view?

How Do I add multipleImages for the top part and have it scrollable without moving the rest of the content? Also, how do I have the little dots at the bottom to indicate which image I'm on? My code for the image is down below.
import SDWebImage
#IBOutlet weak var headerImage: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let imageUrl:NSURL? = NSURL(string: headerPhoto!)
if let url = imageUrl {
headerImage.sd_setImage(with: url as URL!)
}
}
You are looking for something called image slide show or image slider.
Instead of creating them by yourself which requires lots of effort, here is a GitHub library that is easy to use.
Basicly the way slider works is that it is a horizontal scroll view and each cell in the scroll view is an image so that you can scroll. Then, put a fixed page index element on the bottom of the scroll view to tell you which image you are currently at.
To use it, create a UIView in your viewcontroller and set both class and module to ImageSlideshow. Then connect it to your ViewController.swift as IBOutLet.
Then create an array of image urls
let alamofireSource = [AlamofireSource(urlString: "imgurl1")!, AlamofireSource(urlString: "imgurl2")!, AlamofireSource(urlString: "imgurl3")!]
And finally in your viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
slideshow.backgroundColor = UIColor.white
slideshow.slideshowInterval = 5.0
slideshow.pageControlPosition = PageControlPosition.underScrollView
slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
slideshow.pageControl.pageIndicatorTintColor = UIColor.black
slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill
slideshow.setImageInputs(alamofireSource)
}

Swift 3 ios: UILabel needs to appear on top of a UIImage when the Image appears

So I am trying to give the user the ability to design their own virtual business card where the user can drag a label around the UIView, they can change background color of the UIView, as well as the color of the UILabel. The card itself is represented by a nib and the user selects the different color choices or template image choices through a UIPickerView. I want the Label to appear on top of the Image when it appears and is dragged to that part of the View, however it is not currently doing that. Changing colors of the background for the UIView and the text work just fine; I am also able to add and remove the Template image (which is a PNG) by using .isHidden().
My Problem is that when the UIImage is added to the UIView it is on top of the UILabel. If part of the UILabel overlaps with part of the png image it disappears behind it, and I would like it to appear over the image.
The code for the nib is in userCardView.swift and the picker view is on the mainViewController where I set the UserDefaults which works fine.
class userCardView: UIView {
#IBOutlet var cardView: UIView!
#IBOutlet weak var testLBL: UILabel!
#IBOutlet weak var templateImage: UIImageView!
var colorIndex = ["Black", "White", "Gray"]
var templateIndex = ["None", "Triangle"]
override func awakeFromNib() {
super.awakeFromNib()
cardView.clipToBounds = true
let backColorObject = UserDefaults.standard.object(forKey: "backGroundColor") as? String
let textColorObject = UserDefaults.standard.object(forKey: "textColor") as? String
let templateColorObject = UserDefaults.standard.object(forKey: "templateColor") as? String
let templateImageObject = UserDefaults.standard.object(forKey: "templateImage") as? String
for _ in templateIndex {
if templateImageObject == templateIndex[1] {
templateImage.isHidden = false
templateImage.image = UIImage(named: "test_triangle.png")
//testLBL.bringSubview(toFront: templateImage)
templateImage.bringSubview(toFront: testLBL)
} else if templateImageObject == templateIndex[0] {
templateImage.isHidden = true
} else {
templateImage.isHidden = true
}
}
I'm Still pretty new to using swift so any help at all would be greatly appreciated, thank you.
You need to ask testLBL's superview to bring it to the front. Your code templateImage.bringSubview(toFront: testLBL) won't work if templateImage is not the parent view of testLBL because testLBL is not in templateImage's view hierarchy.
So try to use the superview of testLBL
<testLBL's parent view>.bringSubview(toFront: testLBL)
or
testLBL.parentView.bringSubview(toFront: testLBL)
Hope it solves your problem

Swift iOS: pass Image to other VC

I am trying to pass and re-pass an image between two ViewControllers. My approach so far:
if segue.identifier == "chooseCat"
{
let IVC: AddProductVC = segue.destinationViewController as! AddProductVC
IVC.pname2 = pname
IVC.pnick2 = pnick
IVC.pno2 = pno
IVC.podate2 = podate
IVC.pmanu2 = pmanu
IVC.cidnew2 = cidnew
// IVC.imageImage.image = imageImage3.image
}
Edit: To prevent irritations. pname...pnick and the others are variables as strings that are working correctly. The big point is imageImage3.image.
Unfortunately it does not work as the app crashes. Actually I don't really want to pass the image directly to a UIImage; I'd rather have it as data if that's somehow possible. The tricky part is that it is a TableViewController and not a normal ViewController where I could hide imageImage3.image.
In your example you set the image property of an imageImage, which may be an outlet of type UIImageView that does not exist yet! If it is so, then create an UIImage property of your AddProductVC viewcontroller and pass the image to it. And then, when your views are all properly loaded, set up your imageView.
Example : Suggested by #iRealMe
To pass image, you can do like
IVC.image = imageImage3.image
and now in your AddProductVC
var image: UIImage!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = image
}

Resources