简体   繁体   English

当父视图是UIScrollView UNTIL I scoll时,UITextView文本不显示

[英]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.

相关问题 UITextView内部UIScrollView滚动父作为文本输入 - UITextView Inside UIScrollView Scrolling Parent as Text is Entered 更改UITextView中链接的显示文本 - Change displayed text of a link in UITextView UITextView 中的文本不显示在 UIScrollView 中 - Text from UITextView does not display in UIScrollView UIScrollView内的UIScrollView –在文本视图的边界内忽略滚动视图的接触? - UIScrollView inside UIScrollView – ignore scroll view touches when in bounds of text view? 当文本视图可编辑时,iphone UITextView不支持数据检测器 - iphone UITextView does not support data detectors when the text view is editable 大文本在 UIScrollView 内部的 UITextView 中被截断 - Large Text Being Cut Off in UITextView That is Inside UIScrollView 添加UIScrollView作为2 UITextview的超级视图,因此无法滚动视图 - adding a UIScrollView as a superview of 2 UITextview made no view can be scrolled 将文本添加到UITextView时重叠UITextView - Overlapping UITextView when add text to UITextView 当我仅与视图中的特定位置交互时,是否可以使UIScrollView仅滚动? UIScrollView iPad - Can I make a UIScrollView only scroll when i interact only with specific places in the view? UIScrollView iPad 从父视图将UITextView内容传递给ModalViewController - Pass UITextView contents to ModalViewController from parent view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM