我正在尝试转换 Ray Wenderlich UIScrollView Tutorial 的第 1 部分使用通用设备进入自动布局。我目前在导航 Controller 中嵌入了一个 UITableView
,将 ViewController
类的 UIViewController
与 UIScrollView
(没有自定义类)。导航 Controller 有一个不透明的导航栏,没有工具栏。 UIViewController
的属性可以在下面相册的第一张图片中看到。
仅使用四个 Storyboard约束(如下所述),我的 View 看起来就像相册中的第二张图像,scrollView
的帧大小为不正确的 600x536
。为了解决这个问题,我决定在运行时在 ViewController.swift
的 viewDidLoad()
中再次添加约束。我现在总共有 8 个约束:
- Storyboard的 4 个限制:
scrollView.top
=topLayoutGuide.bottom
scrollView.bottom
=bottomLayoutGuide.top
scrollView.leading
=superView.leading
scrollView.trailing
=superView.trailing
- ViewController.swift 中的 4 个约束:
scrollView.top
=topLayoutGuide
scrollView.bottom
=view.bottom
scrollView.leading
=view.leading
scrollView.trailing
=view.trailing
由于这些限制,在缩放之前,我的 View 现在看起来像第三张专辑图像,scrollView
的帧大小仍然不正确的 375x667
。缩放后,我的 View 是正确的,看起来像第四张专辑图像,scrollView
的帧大小为正确的375x603
。其他一些注意事项:
self.topLayoutGuide.length = 0
。不应该是64吗? 20 来自状态栏,44 来自导航栏?- 删除 Storyboard限制会导致与没有程序化限制相同的问题。为什么 Storyboard约束仍然需要存在?
- 我尝试过纯方法和混合方法,但都没有解决这个问题。
- 以及在 Storyboard中禁用
调整 ScrollView 插入
、在顶部栏下延伸边缘
和在不透明栏下延伸边缘
(请参阅第一张图片),我也在代码中禁用它们,但无济于事。 - 我无法使用插图,因为
UIScrollView
的框架在缩放后发生变化。
如何使 UIScrollView
保持恒定、适当的大小?
谢谢。
ViewController 类:UIViewController、UIScrollViewDelegate
@IBOutlet var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//Reset the layout
scrollView.needsUpdateConstraints()
scrollView.setNeedsLayout()
scrollView.layoutIfNeeded()
//Set storyboard settings just in case
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.extendedLayoutIncludesOpaqueBars = false
self.edgesForExtendedLayout = .None
self.automaticallyAdjustsScrollViewInsets = false
self.navigationController?.navigationBar.translucent = false
//Manually added constraints at runtime
var constraintTS = NSLayoutConstraint(item: scrollView, attribute: .Top, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 0)
var constraintBS = NSLayoutConstraint(item: scrollView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0)
var constraintLS = NSLayoutConstraint(item: scrollView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0)
var constraintRS = NSLayoutConstraint(item: scrollView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1.0, constant: 0)
view.addConstraint(constraintTS)
view.addConstraint(constraintBS)
view.addConstraint(constraintLS)
view.addConstraint(constraintRS)
println(scrollView.frame.width)
println(scrollView.frame.height)
//Original Ray Wenderlich code
let image1 = UIImage(named: "photo1")!
imageView = UIImageView(image: image1)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image1.size)
scrollView.addSubview(imageView)
scrollView.contentSize = image1.size
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale
centerScrollViewContents()
}
func centerScrollViewContents() {
let boundsSize = scrollView.bounds.size
var contentsFrame = imageView.frame
if contentsFrame.size.width < boundsSize.width {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.width) / 2.0
} else {
contentsFrame.origin.x = 0.0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.height) / 2.0
println("boundsSize.height: \(boundsSize.height)")
println("boundsSize.width: \(boundsSize.width)")
println("contentsFrame.origin.y: \(contentsFrame.origin.y)")
} else {
contentsFrame.origin.y = 0.0
}
imageView.frame = contentsFrame
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
func scrollViewDidZoom(scrollView: UIScrollView) {
centerScrollViewContents()
}
override func viewWillLayoutSubviews() {
println(self.topLayoutGuide.length)
}
最佳答案
解决了!感谢Working with Top Layout Guide in UIScrollView through Auto Layout 。我不认为 UIScrollView 会让 topLayoutGuide
表现不同,但显然确实如此。事实证明我不需要设置约束; scrollView.layoutIfNeeded()
就可以了!我的scrollView现在是一个常量375x667,消除了调整大小问题并给出self.topLayoutGuide.length
= 64。这允许我通过减去self.topLayoutGuide.length
来垂直居中 View > 在 centerScrollViewContents()
中。我还在 viewWillLayoutSubviews()
中调用 centerScrollViewContents()
。这是我的新代码:
@IBOutlet var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//Redo constraints at runtime
scrollView.layoutIfNeeded()
//Original Ray Wenderlich code
let image1 = UIImage(named: "photo1")!
imageView = UIImageView(image: image1)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image1.size)
scrollView.addSubview(imageView)
scrollView.contentSize = image1.size
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale
}
func centerScrollViewContents() {
let boundsSize = scrollView.bounds.size
var contentsFrame = imageView.frame
if contentsFrame.size.width < boundsSize.width {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.width) / 2.0
} else {
contentsFrame.origin.x = 0.0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.height - self.topLayoutGuide.length) / 2.0
} else {
contentsFrame.origin.y = 0.0
}
imageView.frame = contentsFrame
}
override func viewWillLayoutSubviews() {
println(self.topLayoutGuide.length)
centerScrollViewContents()
}
所有其他功能均未更改。
关于ios - UIScrollView 在自动布局中尺寸不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31552729/