24

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

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

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

4

6 回答 6

63

我想你想要:

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

图表:

|------------------------self.contentSize.width------------------------|
                         |-----self.width-----|
|-------- offset --------|
                                    ^ center
于 2013-03-23T04:57:39.017 回答
12

使用以下代码:

CGFloat newContentOffsetX = (scrollView.contentSize.width - scrollView.frame.size.width) / 2;
scrollView.contentOffset = CGPointMake(newContentOffsetX, 0);
于 2013-03-23T05:02:05.390 回答
8

斯威夫特 4

滚动到内容的中心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)
于 2018-01-29T12:41:52.767 回答
2

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

这是 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)
    }
}
于 2020-04-27T17:08:04.380 回答
1

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

- (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];
}
于 2018-04-23T08:13:43.010 回答
0

斯威夫特 5+

@IBDesignable class CenteredScrollView: UIScrollView {
    
    @IBInspectable var isVerticallyCentered: Bool = false
    @IBInspectable var isHorizontallyCentered: Bool = false

    public func center(vertically: Bool, horizontally:Bool, animated: Bool) {
        
        guard vertically || horizontally else {
            return
        }
        
        var offset = contentOffset
        if vertically && contentSize.height <= bounds.height {
            offset.y = (contentSize.height / 2) - (bounds.size.height / 2)
        }

        if horizontally && contentSize.width <= bounds.width {
            offset.x = (contentSize.width / 2) - (bounds.size.width / 2)
        }
        setContentOffset(offset, animated: animated)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        center(vertically: isVerticallyCentered, horizontally: isHorizontallyCentered, animated: false)
    }
    
}
于 2021-06-02T10:03:11.417 回答