iOS:如何更改UIScrollView内容的大小?

发布于 2024-10-27 08:25:34 字数 699 浏览 1 评论 0原文

我现在正在尝试创建自己的文本面板(带有代码突出显示等)。

事实上,一切都准备好了,除了一件事 - 滚动

我所做的: 创建了 UIScrollView 并使用 Interface builder 将我自己的视图插入为 subview

ViewController viewDidLoad 方法中,我已将初始 textfield 大小指定为 UIScrollView 内容大小。

当用户在我的文本字段中写入过多内容时。我调用委托(即 ViewController)来更改大小。 为此,我只需编写

[scrollView setContentSize:newSize];

“我得到的是滚动视图的增加,但不是我的文本字段 - 仅出现空白行”。我想我也需要设置视图的大小,但我不知道如何...

以及我的问题的第二部分: 每次我调用 [textField setNeedsDisplay] 时,它都会调用 drawRect: 方法,其中矩形等于我的视图的整个大小(比其可见部分大得多)。如果我尝试使用视图修改大小超过 3 MB 的文件,会是什么样子?如何强制它仅重绘可见部分?

感谢您的关注。

I'm now trying to create my own Text panel (with code highlighting and so on).

In fact everything is ready except one thing - scrolling

What I've done:
created UIScrollView and inserted my own view as a subview using Interface builder.

in ViewController viewDidLoad method I've pointed initial textfield size to be the UIScrollView content size.

And when user writes too much into my text field. I call delegate (which is ViewController) to change size.
To do that I simply write

[scrollView setContentSize:newSize];

And what I get is increasing of the scroll view but not my text field - just blank lines appears. I think I need to set my view's size too but I can't figure out how...

And a second part of my question:
Every time I call [textField setNeedsDisplay] it calls drawRect: method with rectangle which is equal to the whole size of my view (which is much bigger than it's visible part). What will it look like if I try to use my view to modify file which size is more than 3 mb? How can I force it to redraw only visible part?

Thanks for your attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月牙弯弯 2024-11-03 08:25:35

第 1 部分

滚动视图的内容大小实际上与其包含的视图的大小或位置无关。如果要更改内容视图的大小以及滚动视图的内容,则需要调用两个不同的方法。

CGSize newSize;
UIScrollView *scrollView;
// assume self is the content view
CGRect newFrame = (CGRect){CGPointZero,newSize}; // Assuming you want to start at the top-left corner of the scroll view. Change CGPointZero as appropriate
[scrollView setContentSize:newSize]; // Change scroll view's content size
[self setFrame:newFrame]; // Change views actual size

第 2 部分

setNeedsDisplay 将整个视图标记为需要显示。要使其仅显示可见部分,您需要使用setNeedsDisplayInRect:visibleRect
假设视图位于左上角(其框架的原点为 0)并且滚动视图不允许缩放,则可以使用滚动视图的内容偏移量和边界大小找到可见矩形。

CGRect visibleRect;
visibleRect.origin = [scrollView contentOffset];
visibleRect.size = [scrollView bounds].size;
[self setNeedsDisplayInRect:visibleRect];

如果只有部分发生变化,您还可以选择绘制可见矩形的一部分。

Part 1:

The scroll view's content size is not actually related to the size or position of the views it contains. If you want to change the size of the content view as well as the scroll view's content, you need to call two different methods.

CGSize newSize;
UIScrollView *scrollView;
// assume self is the content view
CGRect newFrame = (CGRect){CGPointZero,newSize}; // Assuming you want to start at the top-left corner of the scroll view. Change CGPointZero as appropriate
[scrollView setContentSize:newSize]; // Change scroll view's content size
[self setFrame:newFrame]; // Change views actual size

Part 2:

setNeedsDisplay marks the entire view as needing display. To cause it to display only the visible part, you need to use setNeedsDisplayInRect:visibleRect.
Assuming the view is at the top-left corner (its frame's origin is 0) and the scroll view does not allow zooming, the visible rect can be found using the scroll view's content offset and bounds size.

CGRect visibleRect;
visibleRect.origin = [scrollView contentOffset];
visibleRect.size = [scrollView bounds].size;
[self setNeedsDisplayInRect:visibleRect];

You could also choose to draw a part of the visible rect if only part changes.

淡墨 2024-11-03 08:25:35

它比您想象的要简单,例如您的 UIScrollView 中有一个名为“textDesc”的 UITextView,并且您想知道内容的大小,所以下一步将如下所示:

int contentHeight = textDesc.frame.size.height + textDesc.frame.origin.y;

计算完 textDesc 的大小后,只需设置它:

[scrollView setContentSize:(CGSizeMake(320, contentHeight))];

就这样,现在你的 scrollView 知道他里面的文本的大小,并且会自动调整大小,希望这有帮助, 祝你好运。

It's simpler than you think, for example you have an UITextView called "textDesc" inside your UIScrollView, and you want to know the size of content, so next step will looks like that:

int contentHeight = textDesc.frame.size.height + textDesc.frame.origin.y;

After calculating the size of textDesc, just set it:

[scrollView setContentSize:(CGSizeMake(320, contentHeight))];

That's all, now your scrollView know the size of your text inside him, and will resize automatically, hope this helps, good luck.

神回复 2024-11-03 08:25:35

将文本字段 autoresizingMask 属性设置为灵活的高度和宽度:

[textField setAutoResizingMask:UIViewAutoResizingFlexibleHeight | UIViewAutoResizingFlexibleWidth];

当您更改其父级的大小(滚动视图的 contentSize)时,这应该会自动扩展视图,

否则尝试:

[textField setFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];

在更改滚动视图的内容大小之后。

Set your text field autoresizingMask property to flexible height and width:

[textField setAutoResizingMask:UIViewAutoResizingFlexibleHeight | UIViewAutoResizingFlexibleWidth];

This should expand the view automatically when you change it's parent's size (the scrollView's contentSize)

Otherwise try:

[textField setFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];

just after you change the scroll view's contentsize.

只有影子陪我不离不弃 2024-11-03 08:25:35

如果您确实想设置(即“覆盖”)滚动视图的内容大小:

很简单,

someScroll.contentSize = CGSize(width: blah, height: blah)

但是,重点是 - 与设置框架完全相同 - 您必须在 layoutSubviews

除了在layoutSubviews 中之外,这样做完全没有意义。 (就像在布局之外设置框架是毫无意义的一样。)

OP 要求什么..

在这个 QA 中,OP 根本不明白如何设置有问题的实际视图的高度(即标签等)。

请注意,通常您永远不会设置滚动视图的文字属性 .contentSize。 (你只会在极其不寻常和高级的情况下这样做。Apple 甚至将其设置为可写,这可能是一个错误,“正常”编程中没有必要更改它甚至查看它。)

If you truly want to set (ie "override") the content size of a scroll view:

It's simply this,

someScroll.contentSize = CGSize(width: blah, height: blah)

However, the whole point is - exactly like setting a frame - you have to do that in layoutSubviews.

It's completely pointless doing this, other than in layoutSubviews. (Just as it's pointless setting a frame other than in layout.)

What the OP asked ..

In this QA the OP simply didn't understand how to set the height of the actual view in question (ie, the labels, etc).

Note that in general you NEVER, EVER set the literal property .contentSize of a scroll view. (You would only do so in extremely unusual and advanced situations. It's likely a mistake that Apple even made it writable, there is no point in "normal" programming to change it or even see it.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文