ios 如何使用 contentOffset 在 UIScrollView 中居中内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15583515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:50:11  来源:igfitidea点击:

How to center content in a UIScrollView with contentOffset

iosobjective-cuiscrollview

提问by zakdances

This should be a VERY easy problem but I'm having trouble getting the desired result. I have horizontally scrolling UIScrollView with a width of 320. The width of its content is 5000. I'm trying to center the content with:

这应该是一个非常简单的问题,但我无法获得所需的结果。我水平滚动 UIScrollView,宽度为 320。其内容的宽度为 5000。我试图将内容居中:

// 'self' is the UIScrollView
CGFloat newContentOffsetX = (self.width/2) + (self.contentSize.width/2);
self.contentOffset = CGPointMake(newContentOffsetX, 0);

I don't understand why this is not centering the content. Can you see something wrong with the calculations?

我不明白为什么这不是以内容为中心。你能看出计算有什么问题吗?

回答by Isaac

I think you want:

我想你想要:

CGFloat newContentOffsetX = (self.contentSize.width/2) - (self.bounds.size.width/2);

diagram:

图表:

|------------------------self.contentSize.width------------------------|
                         |-----self.width-----|
|-------- offset --------|
                                    ^ center

回答by Apurv

Use below code:

使用以下代码:

CGFloat newContentOffsetX = (scrollView.contentSize.width - scrollView.frame.size.width) / 2;
scrollView.contentOffset = CGPointMake(newContentOffsetX, 0);

回答by Vahid

Swift 4

斯威夫特 4

Scroll to center of content of ScrollView

滚动到内容的中心 ScrollView

let centerOffsetX = (scrollView.contentSize.width - scrollView.frame.size.width) / 2
let centerOffsetY = (scrollView.contentSize.height - scrollView.frame.size.height) / 2
let centerPoint = CGPoint(x: centerOffsetX, y: centerOffsetY)
scrollView.setContentOffset(centerPoint, animated: true)

回答by Aleksey Mazurenko

Not the best solution but might work for you

不是最好的解决方案,但可能对你有用

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self centerScrollViewAnimated:NO];
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    [self centerScrollViewAnimated:YES];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    *targetContentOffset = [self suggestedCenteredContentOffset];
}

- (CGPoint)suggestedCenteredContentOffset {
    CGSize imageSize = self.pickedImage.size;
    CGSize containerSize = self.zoomingScrollView.bounds.size;
    CGPoint result = self.zoomingScrollView.contentOffset;

    if ( self.zoomingScrollView.zoomScale * imageSize.width < containerSize.width )
        result.x = MIN((self.zoomingScrollView.zoomScale * imageSize.width - containerSize.width)/2, 0);

    if ( self.zoomingScrollView.zoomScale * imageSize.height < containerSize.height )
        result.y = MIN((self.zoomingScrollView.zoomScale * imageSize.height - containerSize.height)/2, 0);

    return result;
}

- (void)centerScrollViewAnimated:(BOOL)animated {
    [self.zoomingScrollView setContentOffset:[self suggestedCenteredContentOffset] animated:animated];
}

回答by Dimas Gabriel

I created a simple UIScrollView extension with IBInspectable properties to center content horizontally and/or vertically. It works only when the scrollview content size is lower than the scrollview size.

我使用 IBInspectable 属性创建了一个简单的 UIScrollView 扩展,以水平和/或垂直居中内容。它仅在滚动视图内容大小小于滚动视图大小时才起作用。

Here's the Swift 5.2 code:

这是 Swift 5.2 代码:

import UIKit

@IBDesignable class CenteredScrollView: UIScrollView {
    @IBInspectable var verticallyCentered: Bool = false
    @IBInspectable var horizontallyCentered: Bool = false

    override var contentSize: CGSize {
        didSet {
            if contentSize.height <= bounds.height && verticallyCentered {
                centerVertically()
            }

            if contentSize.width <= bounds.width && horizontallyCentered {
                centerHorizontally()
            }
        }
    }

    private func centerVertically() {
        let yContentOffset = (contentSize.height / 2) - (bounds.size.height / 2)
        contentOffset = CGPoint(x: contentOffset.x, y: yContentOffset)
    }

    private func centerHorizontally() {
        let xContentOffset = (contentSize.width / 2) - (bounds.size.width / 2)
        contentOffset = CGPoint(x: xContentOffset, y: contentOffset.y)
    }
}