I have created a VC with orientation in Landscape in storyboard
I have added an UIIScrollView in it , say, make it: (w)1000, 500 (h) in the VC.
What I wanted to do:
1) Scrolling the image (with high resolution like 1334 x 750) inside ScrollView
2) view the image in ScrollView in landscape mode
To make ScrollView to display the image, I have to do it in `viewDidAppear`
but here the Problems:
1) The Width and height of the `ScrollView` is gone
2) label on top gone.
3) The `ScrollView` Size become small something like 200 x 150 and start from the Top corner like (0,0)
What I need to do to make `scrollview` size like before 1000 x 500?
--- Update --
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var myUIScrollView: UIScrollView!
var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//-- force to landscape mode:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
self.myUIScrollView.maximumZoomScale = 10.0
self.myUIScrollView.minimumZoomScale = 1.0
self.myUIScrollView.delegate = self
imgView = UIImageView(image: UIImage(named: "MyPhoto.png"))
}
override func viewDidAppear(animated: Bool) {
self.myUIScrollView.contentSize = imgView.bounds.size
self.myUIScrollView.addSubview(imgView)
view.addSubview(myUIScollView)
}
override func shouldAutorotate() -> Bool {
return true
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imgView
}
Try this:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var image: UIImage!{
get{
return myImageView.image!
}set{
myImageView.image = newValue
myImageView.sizeToFit()
myScrollView.contentSize = myImageView.frame.size
}
}
var myImageView = UIImageView()
#IBOutlet weak var myScrollView: UIScrollView!{
didSet{
myScrollView.delegate = self
myScrollView.minimumZoomScale = 10.0
myScrollView.maximumZoomScale = 1.0
myScrollView.addSubview(myImageView)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
image = UIImage(named: "one")
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myImageView
}
}
Related
I have an imageview in scrollview (I followed this video https://www.youtube.com/watch?v=0Tz0vI721c8). I want to draw a green dot on that image at x=100, y=100 (from upper left corner of the image). When user zoom in/out and pan, the dot stays at that location (image coord 100,100). How do I do that? I am totally new to ios dev.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupScrollView()
}
func setupScrollView() {
scrollView.delegate = self
}
}
extension ViewController:UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
Create a imageView(dot) to Frame layout, attach dot align top and leading to Frame layout.
Try ur relative object(dot) layout to absolute object(imageView).
let dot = UIView.init()
dot.backgroundColor = .red
//Use layout
dot.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(dot)
let left = dot.leadingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: 100)
let top = dot.topAnchor.constraint(equalTo: imageView.topAnchor, constant: 100)
let width = dot.widthAnchor.constraint(equalToConstant: 10)
let height = dot.heightAnchor.constraint(equalToConstant: 10)
var layout:[NSLayoutConstraint] = [left,top,width,height]
NSLayoutConstraint.activate(layout)
I have an image view as a subview of a UIScrollView in order to enable zooming the image. It somewhat works but not well.
class LargeImageViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
#IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
var selectedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
if let image = selectedImage {
imageView.image = image
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateMinZoomScaleForSize(view.bounds.size)
}
func updateMinZoomScaleForSize(_ size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let minScale = min(widthScale, heightScale)
scrollView.minimumZoomScale = minScale
scrollView.zoomScale = minScale
}
#IBAction func doneTapped(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension LargeImageViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateConstraintsForSize(view.bounds.size)
}
func updateConstraintsForSize(_ size: CGSize) {
let yOffset = max(0, (size.height - imageView.frame.height) / 2)
imageViewTopConstraint.constant = yOffset
imageViewBottomConstraint.constant = yOffset
let xOffset = max(0, (size.width - imageView.frame.width) / 2)
imageViewLeadingConstraint.constant = xOffset
imageViewTrailingConstraint.constant = xOffset
view.layoutIfNeeded()
}
}
Any ideas?
I have now tried following the tutorial suggested and it behaves a little better, but the image is zoomed in and huge.
My constraints are still giving me issues even though I followed the tutorial.
Couple reasons your code is not working like the tutorial.
1 - You missed setting the scroll view delegate (unless you set it in Storyboard). If you did not set it in Storyboard:
override func viewDidLoad() {
super.viewDidLoad()
if let image = selectedImage {
imageView.image = image
}
// add this
scrollView.delegate = self
}
2 - It will still not be quite correct, because the tutorial sets the image in Storyboard, but you're setting it in viewDidLoad(). To fix that:
// remove this
//override func viewWillLayoutSubviews() {
// super.viewWillLayoutSubviews()
// updateMinZoomScaleForSize(view.bounds.size)
//}
// add this
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateMinZoomScaleForSize(scrollView.bounds.size)
updateConstraintsForSize(scrollView.bounds.size)
}
3 - To get rid of the constraint errors in your Storyboard, give the image view Width and Height constraints (such as 100 each), and set them as Placeholders so they will not be used at run-time:
Can't say for sure where the problem is since you've probably configured the storyboard/xib file as well.
However, I recommend you to go through the guide: https://www.raywenderlich.com/5758454-uiscrollview-tutorial-getting-started.
I'm doing official iOS development tutorial from Apple. There is a task to make a zoomable image in a scrollView.
I believe that did it right. There is mistake, since I cant zoom image in.
The task is below in screenshots.
Task part 1
Task part 2
Here is my code:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//set the scroll view's deligate to be the viewcontroller instance
self.scrollView.delegate = self
viewForZooming(in: scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewDidAppear(_ animated: Bool) {
updateZoomFor(size: view.bounds.size)
}
func updateZoomFor(size: CGSize) {
let widthScale = size.width / imageView.bounds.width
let heightScale = size.height / imageView.bounds.height
let scale = min(widthScale, heightScale)
scrollView.minimumZoomScale = scale
scrollView.zoomScale = scale
}
}
Mainscroll is scrollview which was added into another internal UIView and i have UISlider to change internal view scale.
But, MainScroll.setZoomScale(CGFloat(sender.value), animated: true) this line is not work.
==> Here is my code.
override func viewDidLoad() {
super.viewDidLoad()
MainScroll.maximumZoomScale = 1
MainScroll.minimumZoomScale = 10
MainScroll.delegate = self
MainScroll.zoomScale = 5
}
#IBAction func Scrolling(_ sender: UISlider) {
MainScroll.setZoomScale(CGFloat(sender.value), animated: true)
}
Here is a minimal example for getting a UIScrollView to zoom.
The key is that you have to implement func viewForZooming(in scrollView: UIScrollView) -> UIView? and return a view (that is inside your scrollView) that is to be scaled (think of it like your 'document' or 'canvas' view).
Also, notice that zoom is working for the default pinch gestures on the scrollView as well.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
// Assume that outlets exist on a storyboard
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var canvas: UIView! // A view that is added in the scrollView
#IBOutlet var slider: UISlider!
let minScale: CGFloat = 1
let maxScale: CGFloat = 10
let initialScale: CGFloat = 1
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = maxScale
scrollView.zoomScale = initialScale
slider.minimumValue = Float(minScale)
slider.maximumValue = Float(maxScale)
slider.value = Float(initialScale)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return canvas
}
#IBAction func sliderValueChanged(_ sender: UISlider) {
scrollView.zoomScale = CGFloat(sender.value)
}
}
I created a sample project using Xcode 8 with a storyboard having a scroll view and image view as subview of scrollview. image view's content mode is set to center. Scrollview doesn't scroll .
![Here are the constraints in the story board:
scrollView]1
Here is the code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
imageURL = "http://www.hdwallpapers.in/walls/cosmea_floral_bloom-wide.jpg"
}
var imageURL: String? {
didSet {
updateImage()
scrollView.contentSize = imageView.frame.size
}
}
func updateImage() {
if let data = NSData(contentsOf: URL(string:imageURL! )!) {
imageView.image = UIImage(data: data as Data)
imageView.sizeToFit()
}
}
}
However, when I create my imageView programmatically, scrollView works. What am I missing in the above code? code below works
import UIKit
class ImageViewController: UIViewController {
var image: UIImage? {
get {
return imageView.image
}
set {
imageView.image = newValue
imageView.sizeToFit()
}
}
var imageUrl: URL? {
didSet {
image = nil
fetchImage(url: imageUrl)
}
}
func fetchImage(url: URL?) {
let data = NSData(contentsOf: url!)
image = UIImage(data: data as! Data)!
}
var imageView = UIImageView()
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
imageUrl = ImageData.imageURL(for: ImageData.images["imageOne"]!)!
scrollView.addSubview(imageView)
scrollView.contentSize = imageView.frame.size
}
}
You should delete the align center X and align center Y constraints. These constraints are holding your image view in place and not allowing scrolling.
When you delete them you will likely get a constraints error. This is because at design time your layout is ambiguous. The image view's intrinsic size is based on the image, but you want to set your image at run time. To resolve this, you should select your image view, go to the size inspector, click on the intrinsic size drop down menu and set a placeholder size. The placeholder size will only be used at design time and be replaced at run time.