我創建了一個使用Xcode 8的示例項目,其中故事板的滾動視圖和圖像視圖作爲滾動視圖的子視圖。圖像視圖的內容模式設置爲居中。滾動視圖不滾動。當scrollview和圖像視圖都在故事板中時,圖像不會滾動
![Here are the constraints in the story board: scrollView]1
下面是代碼
import UIKit
類的ViewController:UIViewController的{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
imageURL = "http://www.hdwallpapers.in/walls/cosmea_floral_bloom-wide.jpg"
}
var imageURL: String? {
didSet {
updateImage()
scrollView.contentSize = imageView.frame.size
}
}
func updateImage() {
if let data = NSData(contentsOf: URL(string:imageURL!)!) {
imageView.image = UIImage(data: data as Data)
imageView.sizeToFit()
}
}
}
然而,當我編程方式創建我的ImageView,滾動視圖工作。在上面的代碼中我缺少什麼?下面的代碼工作
import UIKit
類ImageViewController:UIViewController的{
var image: UIImage? {
get {
return imageView.image
}
set {
imageView.image = newValue
imageView.sizeToFit()
}
}
var imageUrl: URL? {
didSet {
image = nil
fetchImage(url: imageUrl)
}
}
func fetchImage(url: URL?) {
let data = NSData(contentsOf: url!)
image = UIImage(data: data as! Data)!
}
var imageView = UIImageView()
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
imageUrl = ImageData.imageURL(for: ImageData.images["imageOne"]!)!
scrollView.addSubview(imageView)
scrollView.contentSize = imageView.frame.size
}
}
當您在故事板中設置時,您添加了哪些限制? – beyowulf
我添加了限制 – user3869369