UIScrollView无法以纵向或横向模式显示图像

时间:2015-06-03 10:13:43

标签: ios swift

我在故事板中的Landscape中创建了一个具有方向的VC

我在其中添加了一个UIIScrollView,比如说,在VC中添加:(w)1000,500(h)。

我想做什么:

1)在ScrollView内滚动图像(高分辨率,如1334 x 750) 2)以横向模式ScrollView查看图像


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 }

1 个答案:

答案 0 :(得分:0)

尝试一下::     导入UIKit

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
}

}