2008-09-25 32 views
1

我想要一個UIScrollView帶有一組子視圖,其中每個子視圖都有一個UITextView和不同的文本。對於這個任務,我已經修改了蘋果「iphone開發中心」的PageControl示例,以便將其簡單地UITextView添加到用於生成滾動視圖的子視圖的視圖中。當我運行應用程序(在模擬器和手機上)時,看不到任何文本,但是如果激活「用戶交互」並單擊它,則文本奇蹟般地出現(以及鍵盤)。來自UITextView的文本不會在UIScrollView中顯示

有沒有人有解決方案或在UIScrollView內與UITextView取得任何進展?謝謝。

回答

2

我認爲這個問題源自UITextViewUIScrollView的一個子類別,所以你基本上已經在UIScrollViews中嵌入了滾動視圖。即使文本正確顯示,您也會遇到可用性問題,因爲如果手指滑動應該滾動外部視圖或文本視圖,將永遠不會清楚。

是的,Safari是這樣做的,但它不得不,而且它不是使用Safari的最令人愉快的部分。

我認爲這是其中的困難表明你對系統工作的時間之一。我強烈建議回去重新思考UI。

+0

感謝您的回答,我也認爲這個bug與UITextView是UIScrollView的子類的事實有關,因爲這也發生在UIWebView。但是,我不認爲這樣做是對付系統,因爲它是顯示一組步驟或指令的自然方式 – muesan 2008-10-02 09:46:30

+1

我同意但我不認爲這是一個錯誤,這是一個功能。我沒有看到蘋果公司可以避免這種情況。 – 2009-04-15 11:10:29

-1

它對我有用,方法是將文本值分配放入scrollViewDidScroll方法中。

的示例代碼段:


SAMPLE.h

... 
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate> 
... 

評論: 只是要記住:不要忘記UIScrollViewDelegate協議。


SAMPLE.m

- (void)viewDidLoad { 
     ... whatever is created before and/or after... 

     NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
          Nunc semper lacus quis erat. Cras sapien magna, porta non, 
          suscipit nec, egestas in, arcu. Maecenas sit amet est. 
          Quisque felis risus, tempor eu, dictum ac, volutpat id, 
          libero. Ut gravida, purus vitae interdum elementum, tortor 
          justo porttitor nisi, id rhoncus massa."; 

     // calculate the required frame height according to defined font size and 
     // given text 
     CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0); 
     CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0] 
       constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap]; 
       // for whatever reasons, contraintedToSize seem only be able to 
       // calculate an appropriate height if the input frame height is larger 
       // than required. Means: if your text requires height=250 and input 
       // frame height=100, then this method won't give you the expected 
       // result. 

     frame.size = calcSize; 
     frame.size.height += 0; // calcSize might be not pixel-precise, 
           // so add here additional padding pixels 
     UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame]; 

     // do whatever adjustments 
     tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev 
                  // purpose) 
     self.myTextView = tmpTextView; 
     self.myTextView.editable = NO; 
     self.myTextView.scrollEnabled = NO; 
     self.myTextView.multipleTouchEnabled = NO; 
     self.myTextView.userInteractionEnabled = NO; // pass on events to parentview 
     self.myTextView.font = [UIFont systemFontOfSize:13.0]; 
     [tmpTextView release]; 
     [self.scrollView addSubview:self.myTextView]; 
} 

... 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    // for simplicity text is repeated again, of course it can be a member var/etc... 
    NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
          Nunc semper lacus quis erat. Cras sapien magna, porta non, 
          suscipit nec, egestas in, arcu. Maecenas sit amet est. 
          Quisque felis risus, tempor eu, dictum ac, volutpat id, 
          libero. Ut gravida, purus vitae interdum elementum, tortor 
          justo porttitor nisi, id rhoncus massa."; 
    self.myTextView.text = text; // assign value within this method and it is 
           // painted as expected. 
    } 

評論:

我已經調整了源代碼段與樣品namings和價值明顯。希望沒有錯別字。但是,該代碼還包含文本所需幀高的計算,以防文本值發生變化,因此實際需要不同的幀大小。

將實際的文本值賦值放到scrollViewDidScroll方法中,對於我來說,在滾動等過程中沒有任何類型的閃爍(到目前爲止只在iPhone模擬器中測試過)。

希望有所幫助。當然,我對任何建設性的反饋意見,改進建議甚至解決這個問題的其他方法都是開放的。

0

問題可能是相關的UIScrollViews

我認爲有三種解決方案:

  • 啓用併爲各種控制選擇禁用userInteractionEnabled
  • 假設文本是靜態的,使用的UILabel而不是UITextViewUILabel不是UIScrollView一個子類)
  • 實現你自己的觀點,並得出在drawRect中消息文本自己,而不是依賴於UITextView
1

您可能會遇到UITextView從屏幕外區域滾動到屏幕區域時無法正確更新的問題。

檢查此頁的「畫外UITextViews不正確更新」部分:Multiple Virtual Pages in a UIScrollView

我使用的解決方案是強制的滾動意見重繪時開始出現在屏幕上。這是一個完整的滋擾,但確實解決了這個問題。

7

我決心迫使一個「假」滾動的問題:

textView.contentOffset = CGPointMake(0, 1); 
textView.contentOffset = CGPointMake(0, 0); 
0

我對這個問題的解決辦法是不同的:它只有當我設置的UIScrollView爲關閉的財產「自動調整子視圖」工作

[scrollView setAutoresizesSubviews:NO]; 
相關問題