For the app that I am making I want the user to be able to click an image to make it full screen on the app. And then the user to be able to click the now full screen image to make it the original size.
Is this possible?
Any help would be great, I am just a beginner learner on xcode and am interested in knowing how to do this.
Click on the Image View you added to your View Controller. In the Attributes Inspector on the right, choose your image. Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).
Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.
This is what I found, much easier to setup. Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.
Here is code which creates a full screen image (with black bars to preserve aspect ratio) when an image is clicked.
To use this, add this code to your ViewController which holds the image.
Then, for your imageView that you want to expand, check the box for userInteractionEnabled in the Attributes Inspector, and add a TapGestureRecognizer to it and set it call imageTapped
.
@IBAction func imageTapped(sender: UITapGestureRecognizer) { let imageView = sender.view as! UIImageView let newImageView = UIImageView(image: imageView.image) newImageView.frame = UIScreen.main.bounds newImageView.backgroundColor = .blackColor() newImageView.contentMode = .ScaleAspectFit newImageView.userInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:") newImageView.addGestureRecognizer(tap) self.view.addSubview(newImageView) self.navigationController?.isNavigationBarHidden = true self.tabBarController?.tabBar.isHidden = true } func dismissFullscreenImage(sender: UITapGestureRecognizer) { self.navigationController?.isNavigationBarHidden = false self.tabBarController?.tabBar.isHidden = false sender.view?.removeFromSuperview() }
This code works by creating a new fullscreen image which covers everything else. It has its own TapGestureRecognizer that removes the fullscreen image from its superView (and thus uncovers the original screen).
Update for Swift 3 and 4:
@IBAction func imageTapped(_ sender: UITapGestureRecognizer) { let imageView = sender.view as! UIImageView let newImageView = UIImageView(image: imageView.image) newImageView.frame = UIScreen.main.bounds newImageView.backgroundColor = .black newImageView.contentMode = .scaleAspectFit newImageView.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage)) newImageView.addGestureRecognizer(tap) self.view.addSubview(newImageView) self.navigationController?.isNavigationBarHidden = true self.tabBarController?.tabBar.isHidden = true } @objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) { self.navigationController?.isNavigationBarHidden = false self.tabBarController?.tabBar.isHidden = false sender.view?.removeFromSuperview() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With