当下面的代码在我的UITextView
UIScrollView
中运行时,我在ViewController's
内部viewDidLoad
配置了屏幕外。不幸的是,如果“eventDetails”文本特别长,则UITextView
内不显示任何内容,直到我与之交互为止(例如,点击内部并拖动)。
我的问题:如何使文字显示在UITextView中而不强迫用户首先与UITextView
互动?
以下是代码:
UITextView *txtDetails = [[UITextView alloc] initWithFrame:CGRectMake(-4, yOffset, page3ScrollView.frame.size.width, 0)];
[txtDetails setEditable:NO];
[txtDetails setScrollEnabled:NO];
[txtDetails setFont:[UIFont systemFontOfSize:12]];
[txtDetails resizeAndSetTextWithMaxSize:CGSizeMake(txtDetails.frame.size.width, 999999999) forText:eventDetails withAdditionalHeightOf:16.f];
[page3ScrollView addSubview:txtDetails];
CGRect frame = txtDetails.frame;
frame.size.height = [txtDetails contentSize].height;
txtDetails.frame = frame;
[page3ScrollView setContentSize:CGSizeMake(page3ScrollView.frame.size.width, txtDetails.frame.origin.y + txtDetails.frame.size.height)];
由于
答案 0 :(得分:1)
我能够通过在需要时设置UITextView的文本(例如即将出现在屏幕上)来实现此功能。以下是相关代码:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv
{
if (sv == scrollView) [self updatePagedView];
}
- (void)updatePagedView
{
int currentPage = pageControl.currentPage;
// *** loadDetailsPage3 is where I set the text ***
if (currentPage == 2) {
[self loadDetailsPage3];
}
}
如果有人有更好的解决方案或需要更多解释,请在评论中点击我。
答案 1 :(得分:1)
我遇到了同样的问题,我花了几个小时才找到合适的解决方案......这就是我想出来的......
-(void)DidBecomeActive {
if (!DidUpdateBounds) {
DidUpdateBounds = true; // instance variable set to false on load
NSString *oldtext = uiTextView.text;
//The trick is (1) removing it from it's superview
// (2) resetting it's 'text' property
// (3) re-adding it to the view WHILE it's on-screen.
[uiTextView removeFromSuperview];
uiTextView.text = oldtext;
[self.view addSubview:uiTextView];
[self.view setNeedsDisplay];
}
}
我正在使用 CoreAnimation 切换到此视图。所以在我执行该代码之前,我称之为
//The 0,-300,480,300 is bounds of the UIView my offscreen UITextview is on
[self.view.layer setBounds:CGRectMake(0, -300, 480, 300)];
// SetNeedsDisplay, then call my method above to
//FORCIBILY redrew the UITextView while it's effectively on-screen
[self.view setNeedsDisplay];
[TextLogVC DidBecomeActive];
//CoreAnimation then goes here
这解决了这个问题。在设置了layer.bounds之后,它重新绘制了控件(UITextView
)并且它认为它在屏幕上。然后 CoreAnimation 负责处理从当前UIView
到新UIView
的动画,并在整个动画中显示 uiTextView 中的所有文本。
答案 2 :(得分:0)
看起来您正在设置的视图不会刷新 当您与视图交互时,它们将被刷新,您的更改将呈现给显示...
尝试在[txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay];
的代码后添加viewDidLoad
。
如果有帮助那么我们可能会找到一些更优雅的解决方案......