2014-01-16 24 views
3

我有一個包含顯示靜態文本的UITextview的應用程序。我使用UITextview來滾動文本,這比在UILabel中顯示的要長得多。出於某種原因,iOS 7下UITextview中的文本不會在旋轉後保持滾動狀態。這可以在iOS 6下運行時按預期工作。UITextview文本在iOS7中旋轉後不顯示頂部線條

這可以通過以UITextview創建項目來顯示,該項目以故事板爲中心,邊距爲50左右。然後添加將UITextview固定到主視圖邊緣的約束。可以肯定的文本字段包含足夠的文本,以使滾動:

enter image description here

現在運行的應用程序和文本將被正確地定位在UITextView中,第一行顯示在頂部。旋轉之後,文本將下移:

enter image description here

旋轉回縱向之後,文本還是向下滾動幾行。

所有這一切都完美地工作,如果你在的iOS 6上運行它,我已經嘗試做了很多的研究,並試圖在viewWillLayoutSubviews以下可能的解決方案以使文本留在位置:

[textView sizeToFit]; 
[textView layoutIfNeeded]; 

[textView setContentOffset:CGPointMake(0.0, 0.0)]; 

[textView scrollRectToVisible:CGRectMake(0.0, 0.0, 1.0, 1.0) animated:NO]; 

似乎沒有任何工作。有誰知道如何保持文本的位置?

+0

難道只是我還是這個問題,那種寧靜?對不起,但我懷疑這是一個超級惱人的錯誤,或者我缺少一些簡單的東西。任何洞察力將不勝感激。 – Bob

回答

1

試試這個:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self.textView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 
} 

我測試了在iOS 7.1。

0

我有同樣的問題。它看起來像使用故事板中的UITextView創建了很多小的惱人的錯誤(如this)。我管理通過以編程創建的UITextView要解決的問題:

-(void) viewDidLoad{ 
    self.textView=[[UITextView alloc] init]; 
    self.textView.translatesAutoresizingMaskIntoConstraints=NO; 
    [self.view addSubview:self.textView]; 

    NSDictionary* views = @{@"textView": self.textView}; 
    NSArray* vContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views]; 
    NSArray* hContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views]; 
    [self.view addConstraints:vContrains]; 
    [self.view addConstraints:hContrains]; 

} 
-(void)viewWillLayoutSubviews{ 
    [self.textView layoutIfNeeded]; 
    [self.textView sizeToFit]; 
} 
0

已經嘗試了本iOS上8.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    self.textView.contentInset = UIEdgeInsetsMake(0,0,0,0); 
} 
1

調用的UITextView的scrollRangeToVisible:消息從委託視圖控制器的viewDidLayoutSubviews通知方法。這將調整旋轉後的範圍以及第一印象。

-(void)viewDidLayoutSubviews { 

    [self scrollToTop]; 
} 

-(void) scrollToTop { 
    [self.textView setScrollEnabled:NO]; 
    [self.textView scrollRangeToVisible:NSMakeRange(0.0f, 0.0f)]; 
    [self.textView setScrollEnabled:YES]; 
} 
0

一些上述的類似:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    self.textView.scrollRectToVisible(CGRect(x:0,y:0,width:1,height:1),animated: false) 
    self.textView.layoutIfNeeded() 
    self.textView.sizeToFit() 
}