iOS:如何更改UIScrollView内容的大小? [英] iOS: how to change the size of the UIScrollView content?

查看:225
本文介绍了iOS:如何更改UIScrollView内容的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

in ViewController viewDidLoad 方法我将初始 textfield 大小指向 UIScrollView 内容大小。

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

当用户在我的文本字段中写入太多内容时。我调用delegate( ViewController )来改变大小。
要做到这一点,我只需编写

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...

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

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?

感谢您的关注。

推荐答案

第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

第2部分

setNeedsDisplay 将整个视图标记为需要显示。要使它只显示可见部分,您需要使用 setNeedsDisplayInRect:visibleRect

假设视图位于左上角(其中) frame的原点是0)并且滚动视图不允许缩放,可以使用滚动视图的内容偏移和边界大小找到可见的rect。

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.

这篇关于iOS:如何更改UIScrollView内容的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆