2011-01-13 7 views
1

は私が動的にscrollviewにいくつかのビューを追加すると、あまりにもscrollviewのcontentsizeを増やすが、私はその高さの下部にscrollview をスクロールしたいのです。は、プログラム下部にscrollviewをスクロール - iphone

でscrollRectToVisibleは私には役に立ちません。それは私のiPhoneのスクリーンの目に見える表示にスクロールするだけですが、私はscrollviewのcontentsizeの底に到達したいと思います。

は、誰も私にいくつかのサンプルコードを与えることができますか?

おかげで、
ナビード・バット

答えて

3

使う代わりに、このような何か:

CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentSize].height); 
[yourScrollView setContentOffset:bottomOffset animated:YES]; 

あなたはそれがアニメーションしたくない場合は、単にNOYESを変更します。

+1

より信頼性のある、どうもありがとうございました。 –

5

私は、@ JERのソリューションを少し修正:

if([yourScrollView contentSize].height > yourScrollView.frame.size.height) 
{ 
    CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentSize].height - yourScrollView.frame.size.height); 
    [yourScrollView setContentOffset:bottomOffset animated:YES]; 
} 

(これはUIScrollViewの下部にコンテンツをスクロールしますが、それが行われる必要がある場合にのみ、そうでなければ、アップ/ダウン奇妙なジャンプ効果を得ますあなたがいない場合

はまた、私は気づいマイナスコンテンツの高さからscrollview自体の絶頂は、それが表示されているコンテンツアップ過去をスクロールします。あなたはスウィフトバージョンたい場合

+1

それはうまく動作します:-) – svmrajesh

+0

それは私のために完璧に動作します... :) –

0
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]; 
0

場合、:

scrollView.setContentOffset(CGPointMake(0, max(scrollView.contentSize.height - scrollView.bounds.size.height, 0)), animated: true) 

希望をこのことができます!

0

これは素晴らしい

CGSize contentSize = scrollview.contentSize; 
[scrollview scrollRectToVisible: CGRectMake(0.0, 
              contentSize.height - 1.0, 
              contentSize.width, 
              1.0) 
         animated: YES]; 
関連する問題