1

我面临一个需要我很长时间才能解决的问题。我构建了一个视图,其中包含一个 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;
}

视图的屏幕截图

4

3 回答 3

3

尝试

- (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;

}

源代码

于 2013-06-09T16:20:57.933 回答
0

您需要先设置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;
}
于 2013-06-09T16:02:36.893 回答
0

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

  1. 从情节提要中禁用自动布局
  2. 我使用 addsubview 将 textview 添加到滚动视图
  3. 最后将滚动视图的内容设置为 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)];

希望能帮助到你!

于 2013-06-10T10:04:27.157 回答