簡體   English   中英

正確的方法UIScrollView上的setContentOffset

[英]setContentOffset on UIScrollView the right way

我正在使用此代碼向下滾動我的UIScrollView ,因為我正在從底部添加一個新的UIView ,並且我想向下滾動到它。 我這樣做是這樣的:

CGPoint newOffset = CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y + floorf(bottomAttachmentView.frame.size.height / bottomAttachmentView.multFactor));
[mainScrollView setContentOffset:newOffset animated:YES];

我基本上將新元素的高度添加到UIScrollViewcontentOffsety ,但有時它會滾動到scrollView contentSize之下,並​​且可以滾動。 發生這種情況是因為我在調用上述方法之前修改了contentSize ,並且Scroll View的高度縮小了。

您如何調用setContentOffset以免我的scrollView滾動超出其自己的contentSize 謝謝!

我實際上要做的就是像這樣將UIScrollView滾動到底部:

CGPoint bottomOffset = CGPointMake(0, [mainScrollView contentSize].height - mainScrollView.frame.size.height);
[mainScrollView setContentOffset:bottomOffset animated:YES];

您還可以使用它滾動到scrollView的底部(Swift 4)

let targetRect = CGRect(x: 0, y: mainScrollView.contentSize.height, width: 1, height: 1)
scrollView.scrollRectToVisible(targetRect, animated: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM