1.加入ScrollViewDelegate
2.設定ScrollView的Delegate為UIImageViewController
scrowView.delegate = self
3.設置zomm的方法 要放大縮小的物件為 imageView
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
4.設定最大最小放大值
scrowView.maximumZoomScale = 2.0
scrowView.minimumZoomScale = 0.1
為了讓ipad版本SplitViewController有個Tittle
所以加了一個NavigationViewController在前面
但這樣直接跑會有問題segues的終點就不是UIImageViewController了
所以我們為UiViewController加入一個擴充屬性
如果自己是NavigationController的話 回傳他下一個可見的ViewController
沒有的話回傳自己
extension UIViewController {
var contentViewController: UIViewController{
if let navcon = self as? UINavigationController{
return navcon.visibleViewController ?? self
}else {
return self
}
}
}
這樣prepare segues就不會有問題
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Storyboard.ShowImageSegues {
//這裡就直接取contentViewController
if let imc = segue.destination.contentViewController as? ImageViewController{
let imageName = (sender as? UIButton)?.currentTitle!
imc.ImageURL = DemoURL.NASAImageName(imageName: imageName)
imc.title = imageName
}
}
}