2012-06-27 36 views
1

我有一些內容的滾動視圖。加載內容後,我使用下面的代碼移動到滾動視圖的頂部關於滾動內部UIScrollView

[scrollViewCustom setContentOffset:CGPointMake(0.0, 0.0) animated:NO]; 

它的工作原理。有一個例外:

我有一個UITextView。如果我添加一些文本,那麼滾動視圖永遠不會回到這個代碼的頂部。我必須手動滾動到頂部。

我甚至在向UITextView添加文本後嘗試添加相同的代碼。

任何人都可以幫忙嗎?謝謝。

NB:所以,繼承人的解決方案:

基本上UITextViewUIScrollView派生正因爲如此,每當有一些文字在那裏,它試圖滾動到使文本可見。我已將UITextView更換爲UILabel,它的功能如同魅力!取而代之的

[scrollViewCustom setContentOffset:CGPointMake(0.0, 0.0) animated:NO]; 

回答

0

使用

[scroll scrollRectToVisible:CGRectMake(0,0, 100,100) animated:NO]; 
+0

不,沒有工作:( – Ahsan

+0

發生了什麼事,請粗糙地 –

+0

其相同,使用給定的代碼,它的確如此nt滾動到頂部,我必須手動執行該操作。 – Ahsan

0
- (void)viewDidLoad{ 
scroll.contentSize=CGSizeMake(320, 400); 
} 

- (void)scrollViewToCenterOfScreen:(UIView *)theView { 
CGFloat viewCenterY = theView.center.y; 
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
CGFloat availableHeight; 

    availableHeight = applicationFrame.size.height - 300; 

CGFloat z = viewCenterY - availableHeight/2.0; 
if (z < 0) { 
    z = 0; 
} 
[scroll setContentOffset:CGPointMake(0, z) animated:YES]; 
} 

-(void)textViewDidBeginEditing:(UITextView *)textView{ 
[self scrollViewToCenterOfScreen:scroll]; 

} 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
if (range.length==0) { 
    [scroll setContentOffset:CGPointMake(0, 0) animated:YES]; 
     return NO; 
    } 
} 
0

只需兩步:

// define the area that is initially visible 
scrollViewCustom.frame = CGRectMake(0, 5, 354, 500); 

// then define how much a user can scroll it 
[scrollViewCustom setContentSize:CGSizeMake(354, 960)]; 

In addition,clear the background color of scroll view to show the text. 
scrollViewCustom.backgroundColor = [UIColor clearColor]; 
+0

抱歉的混亂,我可以移動它,但手動,而不是給定的代碼。 – Ahsan