2015-11-08 51 views
1

試圖找出一種方法來製作iPhone的普通照片瀏覽器,但我無法進行縮放工作。如何在縮小時在滾動視圖中居中顯示UIImageView?

myimageview = UIImageView() 
    myimageview.translatesAutoresizingMaskIntoConstraints = false 
    myimageview.image = UIImage(named: "1") 
    scrollview = UIScrollView() 
    scrollview.delegate = self 
    scrollview.backgroundColor = UIColor.brownColor() 
    scrollview.minimumZoomScale = self.view.frame.width/myimageview.image!.size.width 
    scrollview.maximumZoomScale = 1.0 
    scrollview.translatesAutoresizingMaskIntoConstraints = false 

    self.view.addSubview(scrollview) 
    scrollview.addSubview(myimageview) 
    let views = ["scrollview":scrollview,"myimageview":myimageview] 
    //add scrollview constraints 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollview]|", options: [], metrics: nil, views: views)) 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollview]|", options: [], metrics: nil, views: views)) 
    scrollview.contentSize = myimageview.image!.size 
    //add contentview:myimageview constraints 
    let imagesize = myimageview.image!.size 
    //let dd = (self.view.frame.height-imagesize.height*self.view.frame.width/imagesize.width)/2 
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .CenterY, relatedBy: .Equal, toItem: scrollview, attribute: .CenterY, multiplier: 1.0, constant: 0)) 
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Left, relatedBy: .Equal, toItem: scrollview, attribute: .Left, multiplier: 1.0, constant: 0)) 

    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myimageview.image!.size.width)) 
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myimageview.image!.size.height)) 

所以基本上,我有一個滾動視圖,我得到了一個ImageView的,我把它添加到的UIScrollView的子視圖。我手動設置滾動視圖的內容,我設置縮放。我還添加了viewForZoomingInScrollView委託功能。縮放工作正常。

問題: 縮小時,我希望圖像顯示在屏幕中間(uiscrollview),但現在它在頂部。正如您在下面看到的,圖片位於頂部,我希望它位於屏幕中間。看來CenterY不起作用。沒有發現錯誤。我粘貼的代碼在ViewDidLoad函數中。 enter image description here

回答

4

好的,我發現仍然2年後,autolayout不能解決這個問題,你必須手動編寫它。很傷心。

var myimageview:UIImageView! 
var scrollview:UIScrollView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    myimageview = UIImageView() 
    //myimageview.translatesAutoresizingMaskIntoConstraints = false 
    myimageview.image = UIImage(named: "1") 
    myimageview.frame = CGRectMake(0, 0, myimageview.image!.size.width, myimageview.image!.size.height) 
    scrollview = UIScrollView() 
    scrollview.delegate = self 
    scrollview.backgroundColor = UIColor.brownColor() 
    scrollview.minimumZoomScale = self.view.frame.width/myimageview.image!.size.width 
    scrollview.maximumZoomScale = 1.0 
    scrollview.translatesAutoresizingMaskIntoConstraints = false 

    self.view.addSubview(scrollview) 
    scrollview.addSubview(myimageview) 
    let views = ["scrollview":scrollview,"myimageview":myimageview] 
    //add scrollview constraints 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollview]|", options: [], metrics: nil, views: views)) 
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollview]|", options: [], metrics: nil, views: views)) 
    scrollview.contentSize = myimageview.image!.size 

} 

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { 
    // center the image as it becomes smaller than the size of the screen 
    return myimageview 
} 

func scrollViewDidZoom(scrollView: UIScrollView) { 
    let boundsSize = self.scrollview.bounds.size 
    var contentsFrame = self.myimageview.frame 

    if (contentsFrame.size.width < boundsSize.width) { 
     contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0 
    } else { 
     contentsFrame.origin.x = 0.0 
    } 

    if (contentsFrame.size.height < boundsSize.height) { 
     contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0 
    } else { 
     contentsFrame.origin.y = 0.0 
    } 

    myimageview.frame = contentsFrame; 
} 
相關問題