Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在具有较大contentSize的UIScrollView中居中显示UIImageView_Ios_Swift_Uiscrollview_Uiimageview_Offset - Fatal编程技术网

Ios 在具有较大contentSize的UIScrollView中居中显示UIImageView

Ios 在具有较大contentSize的UIScrollView中居中显示UIImageView,ios,swift,uiscrollview,uiimageview,offset,Ios,Swift,Uiscrollview,Uiimageview,Offset,我有一个全屏滚动视图,我在其中添加了一个图像视图作为子视图。我希望imageView在开始时居中并缩放,填充scrollView的大小(即屏幕大小),然后允许用户在两个方向(垂直和水平)上滚动图像,并在左、右、上、下等距 我的意思是:我已经将滚动视图的contentSize设置为CGSize(宽度:screenWidth+200,高度:screenHeight+200),如果我运行应用程序,我看到我只能将这200个偏移点滚动到图像的右侧和底部。我希望图像以内容大小为中心,并且能够水平地向左和向右

我有一个全屏
滚动视图
,我在其中添加了一个
图像视图
作为子视图。我希望
imageView
在开始时居中并缩放,填充
scrollView
的大小(即屏幕大小),然后允许用户在两个方向(垂直和水平)上滚动图像,并在左、右、上、下等距

我的意思是:我已经将滚动视图的
contentSize
设置为
CGSize(宽度:screenWidth+200,高度:screenHeight+200)
,如果我运行应用程序,我看到我只能将这200个偏移点滚动到图像的右侧和底部。我希望图像以内容大小为中心,并且能够水平地向左和向右滚动,每侧偏移100分(垂直滚动时顶部和底部的情况类似)

我怎样才能做到这一点


注意:我在代码中设置所有UI,我不使用故事板或
xib
文件

您只能向下和向右移动,因为您当前的内容偏移量为0,0,所以是左上角-因此您可以向下移动200,向右移动200

您想要的是滚动1/2的垂直填充和1/2的水平填充,因此在您的情况下,您可以执行
scrollView.contentOffset=CGPoint(x:100,y:100)

同样,要使一切正常工作,
UIImageView
的大小必须与
scrollView
的contentSize相同,因此大于屏幕大小

考虑到这些注释,我认为您需要的是图像填充屏幕,然后用户可以滚动到图像的边界之外,然后您只需要将
UIImageView
的大小设置为屏幕的大小,其x和y坐标与
滚动视图的
contentOffset
相同,所以(100100)

以下是示例应用程序执行此操作的视频:

您可能会发现使用约束和自动布局比使用
屏幕宽度
屏幕高度
更容易/更直观:

//
//  CenteredScrollViewController.swift
//  SW4Temp
//
//  Created by Don Mag on 4/18/18.
//

import UIKit

class CenteredScrollViewController: UIViewController {

    let theScrollView: UIScrollView = {
        let v = UIScrollView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.green
        return v
    }()

    let theImageView: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.blue
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the scrollView to the main view
        view.addSubview(theScrollView)

        // add the imageView to the scrollView
        theScrollView.addSubview(theImageView)

        // pin the scrollView to all four sides
        theScrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true
        theScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0).isActive = true
        theScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
        theScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true

        // constrain the imageView's width and height to the scrollView's width and height
        theImageView.widthAnchor.constraint(equalTo: theScrollView.widthAnchor, multiplier: 1.0).isActive = true
        theImageView.heightAnchor.constraint(equalTo: theScrollView.heightAnchor, multiplier: 1.0).isActive = true

        // set the imageView's top / bottom / leading / trailing anchors
        // this *also* determines the scrollView's contentSize (scrollable area)
        // with 100-pt padding on each side
        theImageView.topAnchor.constraint(equalTo: theScrollView.topAnchor, constant: 100.0).isActive = true
        theImageView.bottomAnchor.constraint(equalTo: theScrollView.bottomAnchor, constant: -100.0).isActive = true
        theImageView.leadingAnchor.constraint(equalTo: theScrollView.leadingAnchor, constant: 100.0).isActive = true
        theImageView.trailingAnchor.constraint(equalTo: theScrollView.trailingAnchor, constant: -100.0).isActive = true

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // set the scrollView's contentOffset (to center the imageView)
        theScrollView.contentOffset = CGPoint(x: 100, y: 100)
    }

}
试试这个 Swift 4.*或5*

let maxScale = self.imageScrollView.maximumZoomScale
let minScale = self.imageScrollView.minimumZoomScale

if let imageSize = imageView.image?.size{
    let topOffset: CGFloat = (boundsSize.height - minScale * imageSize.height ) / 2
    let leftOffset: CGFloat = (boundsSize.width - minScale * imageSize.width ) / 2
    self.imageScrollView.contentInset = UIEdgeInsets(top: topOffset, left: leftOffset, bottom: 0, right: 0)
}

谢谢,但实际上我想要的是在图像周围有“空白”偏移,我不希望图像填充滚动视图的内容大小。另一方面,
contentOffset
似乎从一开始就显示了这种偏移。我还想在开始时显示填充屏幕大小的图像,然后能够用这些偏移量滚动它。这是需要的还是我遗漏了什么@AppsDev这就是你想要的吗?这对我来说很有效,核心是在各个方向设置锚,以及宽度和高度锚