只是想找出一种方法来制作一个普通的iphone照片查看器,但我就是无法让缩放工作。
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))
所以基本上,我得到了一个scrollview和一个imageview,并将其添加到uiscrollview的子视图中。我手动设置了ScrollView的ContentSize并设置了缩放。我还添加了viewForZoomingInscrollView委托函数。缩放效果很好。
问题:
缩小时,我希望图像显示在屏幕中间(uiscrollview),但现在它在顶部。正如你在下面看到的,图片在上面,我希望它在屏幕中间。中心似乎不起作用。找不到错误。我粘贴的代码在viewdidload函数中。
最佳答案
好吧,我发现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;
}
关于swift - 缩小时如何使UIImageView在Scrollview中居中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33594250/