UIScrollView: handling of dynamic content? - objective-c

I've a Storyboard with a UIScrollView which contains two UILabels, a UIImageView and a UITextView. The content of the UIImageView and UITextView is dynamic and so are their height.
Currently I'm doing this inside my viewDidLoad to adjust the size of the UITextView after the dynamic text is inserted:
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Is this the way to change its height?
My next problem is to set the content size for the UIScrollView, to activate the scrolling. Is there a smart way to get the height of all its content or do I have to calculate the height for each element and set the sum of this as the content size of the UIScrollView?

IF you had no space in between your objects, you could make a for loop in your scrollView.subviews and add up all the heights to set as the contentSize.
As you probably don't have everything tight together, you're probably better by getting the bottom most object and adding up it's frame.origin.y and it's frame.size.height (maybe you want to have some extra space in here, but that's up to you) and that will give you your contentSize.height to keep everything in there.

Related

UITableView header height changes, but the tableview does not move

In my project, the UITableView header shrinks by about 35 pixels due to the hiding of a UI element.
I can change the UITableView header and all related constraints; but the table view itself never seems to move and stays at the offset of 35 pixels leaving a gap.
Is there something I need to be doing to ensure that the UITableView is always snagging or hugging the bottom of the UITableView header; regardless of what size it might be?
Many thanks
Figured it out, I had to recalculate the new height and set the height of the table view header.
CGRect newFrame = self.tableView.tableHeaderView.frame;
newFrame.size.height = newFrame.size.height - (height-10);
self.tableHeaderView.frame = newFrame;
[self.tableView setTableHeaderView:self.tableHeaderView];

Can't move my UIScrollView

I have a content size 1000x10000 and it's center is on the center of the viewcontroller.
I want to push a button for it to turn pages. In the next code I've tried almost any number in the origin.x but nothing changes the scroll view.
I don't understand the math of it but when I set origin.x=300 and origin.y=100 the view will move up a bit but that's it.
CGRect frame=scroller.frame;
frame.origin.x=ANY NUMBER HERE;
frame.origin.y=0;
[scroller scrollRectToVisible:frame animated:YES];
How can I set it to move the view from right to left
Try this,
[scroller setContentOffset:CGPointMake(300, 0) animated:YES];
setContentOffset:animated:
Sets the offset from the content view’s origin that corresponds to the receiver’s origin.
You should be creating the UIScrollView's frame to the size of the UIViewController like so:
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
Then set the scrollers CONTENT VIEW to the 1000x10000 size like so:
scroller.contentSize = CGSizeMake(1000, 10000);
The contentSize is what makes a UIScrollview able to scroll, as long as the contentsize is larger than the scrollers frame.
If you want to make it scrollable, then contentSize has to be greater than frame.size, otherwise there isn't space to scroll.
Then ensure that scrollEnabled is set to YES (by default it is, so unless that you've changed it it's already set to YES).

How to always makes a UIScrollView scrollable?

I have a UIScrollView. The scrollview.contentSize is set to the height of all subviews in the UIScrollView. When the height of the contentSize if greater than the height of the UIScrollView, it is perfectly scrolling when dragging the view. When the height of the contentSize is less than the height of the UIScrollView nothing is happening when I drag the view.
I think that is standard behavior, since there is really nothing to scroll. But I would like, that the UIScrollView is moving a bit anyway.
A possible solution is to ensure, that the height of the contentSize is never less than the frame.height plus a small margin (i.e. 5px):
CGSize scrollSize = self.frame.size;
int defaultHeight = self.frame.size.height + 5;
int contentHeight = self.webView.frame.origin.y + self.webView.frame.size.height;
scrollSize.height = MAX(defaultHeight, contentHeight);
[self.scrollView setContentSize:scrollSize];
Is there a less hackish way to do this?
There are two properties on UIScrollView that sound like what you want:
#property(nonatomic) BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
#property(nonatomic) BOOL alwaysBounceHorizontal; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally
Simply set one or both of them to YES and you will get the bounce effect.
This can also be set in IB by checking the "Bounce Horizontally" and/or "Bounce Vertically" box(s):
In swift:
scrollView.alwaysBounceVertical = true
scrollView.alwaysBounceHorizontal = true

How to make vertical CCScrollView align to top?

I have a vertical CCScrollView on iOS where height is not divisible by cell height.
So, cells align to the bottom of CCScrollView.
Is there any way to make CCScrollView align to top?
I have not used the CCScrollView, but I have tried another UIScrollView implementation for cocos2d and I think the same way I solved this could apply. Set your scrollview's contentOffset to ccp(0.0, scrollview.contentSize.height - scrollview.size.height) instead of ccp(0.0, 0.0). Then, adjust the position of your content based on your scrollview's contentSize. For instance if you wanted your content's position to be ccp(x, y) as it appears, you could do something like
content.position = CGPointMake(x, scrollview.contentSize.height - scrollview.size.height + y)
size is the view size of the scrollview, but I'm not sure what the actual property for scrollview is since I haven't used it.
Use below code works for me:
[self.scrollView setContentOffset:ccp(0, -self.scrollView.container.contentSize.height + self.scrollView.boundingBox.size.height) animated:NO];

How to make height of OHAttributedLabel scale with content height?

I use an OHAttributedLabel called demoLbl for displaying text with formatted areas. This label is laid out with Interface Builder and is connected to a property in my ViewController. After setting the attributedText to the label I want all the text to be displayed in the label.
If I don't resize the label then the text is cropped at the end of the label so the rest of the text is missing.
If I use [demoLbl sizeToFit]; then the height of the label is larger or smaller in height than the text (about 10 point, varying with the text's length) thus giving me blank areas at the bottom of my view (after scrolling) plus the width of the label is increased by about 2 points.
If I calculate the height of the original text (NSString) before putting it in a NSAttributedString and adding it to the label's attributedText property then the calculated height is way too small for setting it as the label's height.
Is there a hack or trick I can apply so that the label's height is adjusted according to the NSAttributedString's height?
PS: To be more specific I wanted to add OHAttributedLabel as a tag but it's not allowed to me yet.
I'm the author of OHattributedLabel.
I made some fixes recently about my computation of the size. Please check it out it will probably solve your issue.
I also added a method named sizeConstrainedToSize:fitRange: in NSAttributedString+Attributes.h that returns the CGSize of a given NSAttributedString (quite the same way UIKit's sizeWithFont:constrainedToSize: works, but for Attributed strings and CoreText and not plain stings an UIKit)
Actually OHAttributedLabel's sizeThatFits: calls this method itself now.
You can see if this category gives you a more reliable height.
https://gist.github.com/1071565
Usage
attrLabel.frame.size.height = [attrLabel.attributedString boundingHeightForWidth:attrLabel.frame.size.width];
I added this code to the implementation of the OHAttributedLabel class:
// Toni Soler - 02/09/2011
// Overridden of the UILabel::sizeToFit method
- (void)sizeToFit
{
// Do not call the standard method of the UILabel class, this resizes the frame incorrectly
//[super sizeToFit];
CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
CGRect frame = self.frame;
frame.size = [self sizeThatFits:constraint];
[self setFrame:frame];
}
// End Toni Soler - 02/09/2011
Thank you Olivier for sharing your code!