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函數中。