【发布时间】:2015-07-22 02:12:25
【问题描述】:
我正在尝试将Ray Wenderlich UIScrollView Tutorial 的第 1 部分转换为具有通用设备的自动布局。我目前在导航控制器中嵌入了一个UITableView,将ViewController 类的UIViewController 与UIScrollView(没有自定义类)连接起来。导航控制器有一个不透明的导航栏,没有工具栏。 UIViewController 的属性可以在下面相册的第一张图片中看到。
只有四个故事板约束(如下所述),我的视图看起来像相册中的第二张图片,scrollView 的帧大小不正确600x536。为了解决这个问题,我决定在运行时再次添加约束,在viewDidLoad() 和ViewController.swift 中。我现在总共有 8 个约束:
- 故事板上的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
-
有了这些限制,在缩放之前,我的视图现在看起来像第三张专辑图片,scrollView 的帧大小仍然不正确375x667。放大后,我的视图是正确的,看起来像第四张专辑图像,scrollView 的帧大小在正确的375x603 上。其他一些注意事项:
-
self.topLayoutGuide.length = 0。不应该是64吗? 20 来自状态栏,44 来自导航栏? - 删除情节提要约束会导致与没有程序约束相同的问题。为什么故事板约束仍然需要存在?
- 我尝试了纯方法和混合方法,但都没有解决这个问题。
- 除了在情节提要中禁用
Adjust Scroll View Insets、Extend Edges Under Top Bars和Extend Edges Under Opaque Bars(见第一张图片),我还在代码中禁用它们,但无济于事。 - 我不能使用插图,因为
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)
}
【问题讨论】:
标签: ios swift uiscrollview autolayout