【问题标题】:Dynamic UITextView in a dynamic UIScrollView - iOS动态 UIScrollView 中的动态 UITextView - iOS
【发布时间】:2013-06-09 14:41:23
【问题描述】:

我正面临一个需要很长时间才能解决的问题。 我构建了一个视图,其中包含一个 ScrollView。在 ScrollView 中,有一个包含图像的视图和一个 UITextView。文本视图应处于动态高度,禁用滚动。 textview 获取所有文本,但将其切断,并仅显示适合高度的文本。另外,ScrollView 没有变化。

- (void)viewDidLoad

 ....
 //sets the text to the textview
 [self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
 //configures the scrollView
 [self configureScrollView];

 ....

- (void)configureScrollView {

[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];

CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;

[self.contentView sizeToFit];
[self.scrollView sizeToFit];

self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;

 //enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;

我做了很多改变,我不知道里面发生了什么!...

帮助会很好:)

更新-

- (void)configureScrollView {

[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];

CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;

[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}

【问题讨论】:

标签: iphone ios objective-c uitextview


【解决方案1】:

试试

- (void)configureScrollView{

    self.contentView.autoresizingMask = UIViewAutoresizingNone;
    self.contentArticle.autoresizingMask = UIViewAutoresizingNone;

    CGRect textViewFrame = self.contentArticle.frame;
    textViewFrame.size = [self.contentArticle contentSize];
    self.contentArticle.frame = textViewFrame;

    CGRect contentViewFrame = self.contentView.frame;
    contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
    self.contentView.frame = contentViewFrame;

    [self.scrollView setContentSize:contentViewFrame.size];

    self.contentArticle.editable=NO;
    self.contentArticle.scrollEnabled=NO;

    //enable zoomIn
    self.scrollView.delegate=self;
    self.scrollView.minimumZoomScale=1;
    self.scrollView.maximumZoomScale=7;

}

Source code

【讨论】:

  • 我更新了我的问题...部分代码确实帮助了我,但滚动视图的高度仍然不合适,它保持不变。当我使用您的代码时,没有更改,文本视图从视图中间开始......有什么想法吗?
  • @AndroAid 如果您以编程方式调整视图大小,请不要设置自动布局/自动调整掩码。如果您正在设置,请在 containerView 中设置您可以参考提供的源代码。
  • 我想不通...我有一个 UIView,它包含一个 UIScrollView。 scrollView 包含 contentView,其中包含 textview。在所有视图中都检查了 autoResize 子视图。但仍然是 NADA。我的项目快完成了,这是最后一个主要问题:/
  • 添加了问题视图的屏幕截图 :)
  • @AndroAid 我能想到的一个原因是使用AutoLayout。我提供的源代码没有使用自动布局。
【解决方案2】:

有同样的问题,尝试了很多天找到解决方案,最后我得到了它的工作。 我在滚动视图中有一个文本视图。

  1. 从情节提要中禁用自动布局
  2. 我用 addsubview 将 textview 添加到 scrollview 中
  3. 最后将scrollview的内容设置为textview框架高度。

在我的代码下面:

_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;

[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];

希望对你有帮助!

【讨论】:

    【解决方案3】:

    你需要先设置self.contentView高度使用这个代码:

    - (void)configureScrollView {
    
        [self.contentView addSubview:self.contentArticle];
        CGRect frame = self.contentArticle.frame;
        frame.size.height = self.contentArticle.contentSize.height;
        self.contentArticle.frame = frame;
    
        [self.scrollView addSubview:self.contentView];
        frame = self.contentView.frame;
        frame.size.height += self.contentArticle.frame.height;
        self.contentView.frame = frame;
    
        self.scrollView.contentSize = self.contentView.frame.size;
        self.contentArticle.editable=NO;
        self.contentArticle.scrollEnabled=NO;
    
        //enable zoomIn
        self.scrollView.delegate=self;
        self.scrollView.minimumZoomScale=1;
        self.scrollView.maximumZoomScale=7;
    }
    

    【讨论】:

    • 感谢您的回答。当我写信给 Anupdas 时,代码看起来是正确的,但是 textview 中的文本从视图的中间开始。有很多滚动,直到它到达文本,然后文本在中间切断。
    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多