试图找出一种为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的内容大小,然后设置缩放。我还添加了viewForZoomingInScrollView委托函数。缩放工作正常。
问题: 缩小时,我希望图像显示在屏幕中间(uiscrollview),但现在它位于顶部。如下图所示,图片位于顶部,我希望它位于屏幕中间。似乎CenterY不起作用。没有发现错误。我粘贴的代码位于ViewDidLoad函数中。
答案 0 :(得分: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;
}