UIScrollView를 프로그래밍 방식으로 맨 아래로 스크롤합니다.
a게 를 만들려면 해야 하나요?UIScrollView내 코드의 맨 아래까지 스크롤할 수 있습니까?아니면 좀 더 일반적인 방식으로, 하위 뷰의 어느 지점까지요?
UIScroll View를 할 수 ★setContentOffset:animated:콘텐츠 보기의 원하는 부분으로 스크롤할 수 있습니다.음음음음 scroll scroll scroll scroll scroll scroll scroll scroll scroll scroll scroll scroll scroll scroll는는 는 scroll는View라고 입니다.self.scrollView:
목표-C:
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];
신속:
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)
간단한 복사 붙여넣기를 위해 승인된 답변의 빠른 버전:
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)
가장 심플한 솔루션:
[scrollview scrollRectToVisible:CGRectMake(scrollview.contentSize.width - 1,scrollview.contentSize.height - 1, 1, 1) animated:YES];
신속한 구현:
extension UIScrollView {
func scrollToBottom(animated: Bool) {
if self.contentSize.height < self.bounds.size.height { return }
let bottomOffset = CGPoint(x: 0, y: self.contentSize.height - self.bounds.size.height)
self.setContentOffset(bottomOffset, animated: animated)
}
}
사용:
yourScrollview.scrollToBottom(animated: true)
기존 답변에 대한 개선 사항일 뿐입니다.
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];
하단 삽입도 가능합니다(키보드가 보일 때 스크롤 표시를 조정하는 경우).
콘텐츠 오프셋을 콘텐츠크기의 높이로 설정하는 것은 올바르지 않습니다.콘텐츠의 하단을 스크롤 뷰의 맨 위로 스크롤하기 때문에 보이지 않습니다.
올바른 해결책은 다음과 같이 콘텐츠의 하단을 스크롤 뷰의 하단으로 스크롤하는 것입니다.sv[ : u [ UIScroll View ]입니다.
CGSize csz = sv.contentSize;
CGSize bsz = sv.bounds.size;
if (sv.contentOffset.y + bsz.height > csz.height) {
[sv setContentOffset:CGPointMake(sv.contentOffset.x,
csz.height - bsz.height)
animated:YES];
}
2.2를 2. contentInset에 넣다
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)
이것은 내선번호여야 합니다.
extension UIScrollView {
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
}
경우 ''가 '있다'는 것을 확인할 수 .bottomOffset.y > 0 전
면 어쩌지contentSize bounds무슨 일입니까?
Swift의 경우:
scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0) ), animated: true)
맨 위로 스크롤
- CGPoint topOffset = CGPointMake(0, 0);
- [scrollView setContentOffset:topOffset animated:YES];
맨 아래로 스크롤
- CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - self.scrollView.bounds.size.height);
- [scrollView setContentOffset:bottomOffset animated:YES];
여기 있는 모든 답변들이 안전 지역을 고려하지 않은 것 같습니다.iOS 11 i i i X i i 。는 스크롤 뷰의 뷰에 을 줄 수 .contentInset.
iOS 11 이상에서는 콘텐츠를 삽입한 상태로 아래까지 올바르게 스크롤합니다. 하면 됩니다.adjustedContentInsetcontentInset 다음 코드를 확인합니다.
- 신속:
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height + scrollView.adjustedContentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)
- 목표-C
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.adjustedContentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];
- 한 확장(원래의 확장)을 합니다).
contentOffset.x
extension UIScrollView {
func scrollsToBottom(animated: Bool) {
let bottomOffset = CGPoint(x: contentOffset.x,
y: contentSize.height - bounds.height + adjustedContentInset.bottom)
setContentOffset(bottomOffset, animated: animated)
}
}
참고 자료:
UITableView(UIScrollView의 서브 클래스)를 사용하고 있는 경우, 이것을 실시하기 위한 다른 유용한 방법도 찾았습니다.
[(UITableView *)self.view scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
UIScroll View ★★setContentOffset:animated:재빠르다
let bottomOffset : CGPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)
scrollView contentSize를 변경하는 경우(예를 들어 scrollView 내부에 있는 stackView에 무언가를 추가하는 경우)에 호출해야 합니다.scrollView.layoutIfNeeded()이치노
예:
scrollView.layoutIfNeeded()
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom)
if(bottomOffset.y > 0) {
scrollView.setContentOffset(bottomOffset, animated: true)
}
(「」)가 있는 footerView ★★★★★★★★★★★★★★★★★」contentInset츠키다
CGPoint bottomOffset = CGPointMake(0, _tableView.contentSize.height - tableView.frame.size.height + _tableView.contentInset.bottom);
if (bottomOffset.y > 0) [_tableView setContentOffset: bottomOffset animated: YES];
신속:
다음과 같은 내선번호를 사용할 수 있습니다.
extension UIScrollView {
func scrollsToBottom(animated: Bool) {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height)
setContentOffset(bottomOffset, animated: animated)
}
}
용도:
scrollView.scrollsToBottom(animated: true)
발디르, 이게 도움이 되길 바래
CGPoint bottomOffset = CGPointMake(0, [textView contentSize].height - textView.frame.size.height);
if (bottomOffset.y > 0)
[textView setContentOffset: bottomOffset animated: YES];
구조대상이군!
이것을 공유 유틸리티 헤더에 추가합니다.
@interface UIScrollView (ScrollToBottom)
- (void)scrollToBottomAnimated:(BOOL)animated;
@end
다음으로 유틸리티 구현에 대해 설명하겠습니다.
@implementation UIScrollView(ScrollToBottom)
- (void)scrollToBottomAnimated:(BOOL)animated
{
CGPoint bottomOffset = CGPointMake(0, self.contentSize.height - self.bounds.size.height);
[self setContentOffset:bottomOffset animated:animated];
}
@end
그 후, 예를 들면 다음과 같이, 원하는 장소에 실장할 수 있습니다.
[[myWebView scrollView] scrollToBottomAnimated:YES];
수평 스크롤 뷰의 경우
수평 Scroll View가 있고 그 끝까지 스크롤하려면(내 경우 오른쪽 상단) 승인된 답변의 일부를 변경해야 합니다.
목표-C
CGPoint rightOffset = CGPointMake(self.scrollView.contentSize.width - self.scrollView.bounds.size.width + self.scrollView.contentInset.right, 0 );
[self.scrollView setContentOffset:rightOffset animated:YES];
재빠르다
let rightOffset: CGPoint = CGPoint(x: self.scrollView.contentSize.width - self.scrollView.bounds.size.width + self.scrollView.contentInset.right, y: 0)
self.scrollView.setContentOffset(rightOffset, animated: true)
컨텐츠의 맨 아래를 볼 수 있도록 하는 좋은 방법은 다음 공식을 사용하는 것입니다.
contentOffsetY = MIN(0, contentHeight - boundsHeight)
이렇게 하면 컨텐츠의 아래쪽 가장자리가 항상 보기의 아래쪽 가장자리 또는 그 위에 있게 됩니다.그MIN(0, ...)이 필요하기 때문입니다.UITableView(아마도UIScrollView)가 보증합니다.contentOffsetY >= 0사용자가 눈에 띄게 스냅하여 스크롤하려고 할 때contentOffsetY = 0사용자에게는 상당히 이상하게 보입니다.
이를 구현하기 위한 코드는 다음과 같습니다.
UIScrollView scrollView = ...;
CGSize contentSize = scrollView.contentSize;
CGSize boundsSize = scrollView.bounds.size;
if (contentSize.height > boundsSize.height)
{
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.y = contentSize.height - boundsSize.height;
[scrollView setContentOffset:contentOffset animated:YES];
}
애니메이션이 필요 없는 경우 다음 작업을 수행할 수 있습니다.
[self.scrollView setContentOffset:CGPointMake(0, CGFLOAT_MAX) animated:NO];
하는 동안에Matt셋업된 컬렉션 뷰가 있다면 솔루션도 고려해야 할 것 같습니다.
적용된 코드는 다음과 같습니다.
CGSize csz = sv.contentSize;
CGSize bsz = sv.bounds.size;
NSInteger bottomInset = sv.contentInset.bottom;
if (sv.contentOffset.y + bsz.height + bottomInset > csz.height) {
[sv setContentOffset:CGPointMake(sv.contentOffset.x,
csz.height - bsz.height + bottomInset)
animated:YES];
}
신속한 처리:
if self.mainScroll.contentSize.height > self.mainScroll.bounds.size.height {
let bottomOffset = CGPointMake(0, self.mainScroll.contentSize.height - self.mainScroll.bounds.size.height);
self.mainScroll.setContentOffset(bottomOffset, animated: true)
}
표의 마지막 항목까지 스크롤하는 솔루션 보기:
Swift 3 :
if self.items.count > 0 {
self.tableView.scrollToRow(at: IndexPath.init(row: self.items.count - 1, section: 0), at: UITableViewScrollPosition.bottom, animated: true)
}
여기서 설명한 바와 같이 https://janeshswift.com/ios/swift/how-to-scroll-to-a-position-programmatically-in-uiscrollview/
커스텀을 생성할 수 있습니다.UIScrollView로서의 확장.
extension UIScrollView {
func scrollToTop(animated: Bool = false) {
setContentOffset(CGPoint(x: contentOffset.x, y: -adjustedContentInset.top), animated: animated)
}
var bottomContentOffsetY: CGFloat {
max(contentSize.height - bounds.height + adjustedContentInset.bottom, -adjustedContentInset.top)
}
func scrollToBottom(animated: Bool = false) {
setContentOffset(CGPoint(x: contentOffset.x, y: bottomContentOffsetY), animated: animated)
}
func scrollToLeading(animated: Bool = false) {
setContentOffset(CGPoint(x: -adjustedContentInset.left, y: contentOffset.y), animated: animated)
}
var trailingContentOffsetX: CGFloat {
max(contentSize.width - bounds.width + adjustedContentInset.right, -adjustedContentInset.left)
}
func scrollToTrailing(animated: Bool = false) {
setContentOffset(CGPoint(x: trailingContentOffsetX, y: contentOffset.y), animated: animated)
}
func scrollViewToVisible(_ view: UIView, animated: Bool = false) {
scrollRectToVisible(convert(view.bounds, from: view), animated: true)
}
var isOnTop: Bool {
contentOffset.y <= -adjustedContentInset.top
}
var isOnBottom: Bool {
contentOffset.y >= bottomContentOffsetY
}
}
사용처:
DispatchQueue.main.async {
self.itemsScrollView.scrollToBottom()
}
나한테는 안 통했어, 내가 그걸 쓰려고 했을 때UITableViewController에self.tableView (iOS 4.1)추가 후footerView테두리 밖으로 스크롤 되어 검은 화면이 표시됩니다.
대체 솔루션:
CGFloat height = self.tableView.contentSize.height;
[self.tableView setTableFooterView: myFooterView];
[self.tableView reloadData];
CGFloat delta = self.tableView.contentSize.height - height;
CGPoint offset = [self.tableView contentOffset];
offset.y += delta;
[self.tableView setContentOffset: offset animated: YES];
CGFloat yOffset = scrollView.contentOffset.y;
CGFloat height = scrollView.frame.size.height;
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat distance = (contentHeight - height) - yOffset;
if(distance < 0)
{
return ;
}
CGPoint offset = scrollView.contentOffset;
offset.y += distance;
[scrollView setContentOffset:offset animated:YES];
나는 그것을 발견했다.contentSize텍스트의 실제 크기를 반영하지 않기 때문에 아래로 스크롤하려고 하면 조금 어긋납니다.실제 콘텐츠 크기를 확인하는 가장 좋은 방법은 실제로NSLayoutManager의usedRectForTextContainer:방법:
UITextView *textView;
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
에 실제로 표시되는 텍스트의 양을 확인하려면UITextView프레임 높이에서 텍스트 컨테이너 삽입을 빼서 계산할 수 있습니다.
UITextView *textView;
UIEdgeInsets textInsets = textView.textContainerInset;
CGFloat textViewHeight = textView.frame.size.height - textInsets.top - textInsets.bottom;
그러면 스크롤이 쉬워집니다.
// if you want scroll animation, use contentOffset
UITextView *textView;
textView.contentOffset = CGPointMake(textView.contentOffset.x, textSize - textViewHeight);
// if you don't want scroll animation
CGRect scrollBounds = textView.bounds;
scrollBounds.origin = CGPointMake(textView.contentOffset.x, textSize - textViewHeight);
textView.bounds = scrollBounds;
에 를 나타내는 입니다.UITextView.
textView.frame.size = (width=246, height=50)
textSize = (width=10, height=16.701999999999998)
textView.contentSize = (width=246, height=33)
textView.textContainerInset = (top=8, left=0, bottom=8, right=0)
UIScrollView를 확장하여 스크롤 ToBottom 메서드를 추가합니다.
extension UIScrollView {
func scrollToBottom(animated:Bool) {
let offset = self.contentSize.height - self.visibleSize.height
if offset > self.contentOffset.y {
self.setContentOffset(CGPoint(x: 0, y: offset), animated: animated)
}
}
}
맨 아래까지 스크롤하려면 대상 뷰의 최대 높이로 작업해야 합니다.
import UIKit
extension UIScrollView {
func scrollToBottomOf(targetView: UIView, animated: Bool) {
setContentOffset(CGPoint(x:targetView.frame.minX, y:targetView.frame.maxY), animated: animated)
}
}
//func invocation example
optionScrollView.scrollToBottomOf(targetView: self.optionsStackView, animated: false)
자마린.iOS 버전:UICollectionView 및 중
var bottomOffset = new CGPoint (0, CollectionView.ContentSize.Height - CollectionView.Frame.Size.Height + CollectionView.ContentInset.Bottom);
CollectionView.SetContentOffset (bottomOffset, false);
언급URL : https://stackoverflow.com/questions/952412/uiscrollview-scroll-to-bottom-programmatically
'programing' 카테고리의 다른 글
| Swift 옵션 값은 무엇입니까? (0) | 2023.04.22 |
|---|---|
| Excel 또는 스프레드시트 열 문자를 피토닉 방식으로 숫자로 변환 (0) | 2023.04.22 |
| "PowerShell 호스트를 초기화하지 못했습니다"로 인해 nuget 패키지를 설치할 수 없습니다. (0) | 2023.04.22 |
| 다른 열의 값을 기반으로 새 열 생성/팬더에서 여러 열의 함수를 적용 (0) | 2023.04.22 |
| IIS: 아이돌타임아웃과 재활용 (0) | 2023.04.22 |