leshka
leshka

Reputation: 1794

iOS: how to change the size of the UIScrollView content?

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.

Upvotes: 6

Views: 31323

Answers (4)

Fattie
Fattie

Reputation: 12621

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

Upvotes: -1

Resty
Resty

Reputation: 101

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.

Upvotes: 1

ughoavgfhw
ughoavgfhw

Reputation: 39905

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.

Upvotes: 12

Jonathan.
Jonathan.

Reputation: 55554

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.

Upvotes: 0

Related Questions