[英]UITextView text not displayed when parent view is UIScrollView UNTIL I scoll
I have a UITextView
inside of a UIScrollView
that is configured offscreen when the code below runs in my ViewController's
viewDidLoad
. 我在
UIScrollView
中有一个UITextView
,当我在ViewController's
viewDidLoad
中运行下面的代码时,它在屏幕外配置。 Unfortunately, if the "eventDetails" text is particularly long nothing is displayed inside the UITextView
until I interact with it (eg click inside and drag for example). 不幸的是,如果“eventDetails”文本特别长,则在我与
UITextView
交互之前不会显示任何内容(例如,单击内部并拖动)。
My question: How to have it so the text is displayed in the UITextView WITHOUT forcing the user to interact with the UITextView
first? 我的问题:如何拥有它以便文本显示在UITextView中而不强迫用户首先与
UITextView
进行交互?
Here is the code: 这是代码:
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)];
Thanks 谢谢
I was able to get this working by setting the UITextView's text ONLY when needed (eg is about to come on screen). 通过在需要时设置UITextView的文本(例如即将出现在屏幕上),我能够实现这一点。 Below is the relevant code:
以下是相关代码:
- (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];
}
}
If anyone has a better solution or needs more explanation just hit me up in the comments. 如果有人有更好的解决方案或需要更多解释,请在评论中点击我。
I've had the same issue and it's taken me several hours to find a suitable fix for it... Here's what I came up with... 我有同样的问题,我花了几个小时找到合适的解决方案...这就是我想出的......
-(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];
}
}
I'm using CoreAnimation to switch to this view. 我正在使用CoreAnimation切换到此视图。 So right before I execute that code, I call this
所以在我执行该代码之前,我称之为
//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
This solves the issue. 这解决了这个问题。 After setting the layer.bounds it redraws the control(
UITextView
) and it thinks it's onscreen. 在设置了layer.bounds之后,它重新绘制了控件(
UITextView
)并且它认为它在屏幕上。 Then CoreAnimation takes care of animating from my current UIView
to the new UIView
and all of the text in the uiTextView is displayed throughout the animation. 然后CoreAnimation处理从我当前的
UIView
到新UIView
的动画,并且在整个动画中显示uiTextView中的所有文本。
Looks like the views that you are setting up are not refreshed. 看起来您正在设置的视图不会刷新。
And when you interact with the views then they are refreshed and your changes are rendered to the display... 当您与视图交互时,它们将被刷新,您的更改将呈现给显示...
Try adding [txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay];
尝试添加
[txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay];
[txtDetails setNeedsDisplay]; [page3ScrollView setNeedsDisplay];
after your code in viewDidLoad
. 在
viewDidLoad
的代码之后。
If it will help then maybe we'll find some more elegant solution... 如果有帮助那么我们可能会找到一些更优雅的解决方案......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.