UIScrollView Move Resizes UITextView - ios

I have a strange issue, I am using IOS6 and I am trying to get a UIScrollView to resize based upon the content within it, there are 3 UITextView (2 fixed and 1 dynamic height). The problem seems to be that when you scroll down the scroll view the UITextView with the dynamic height will revert back to the previous height set in Interface Builder. I have placed the code below to show how I am setting the scroll height and UITextView height.
- (void)viewDidAppear:(BOOL)animated {
CGRect frame = description.frame;
frame.size = description.contentSize;
description.frame = frame;
CGFloat scrollViewHeight = 0.0f;
scrollViewHeight = 160 + description.contentSize.height;
NSLog(#"%f",description.contentSize.height);
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
}
To expand it seems that the UITextView will not expand into the scrollable area of the UIScrollView, instead it only goes to the initially set height of the scrollview

It turns out that it was the layout constraints that were causing the issue.

Related

how make the UIView height equal to its subview's additional height, so that it can easily scroll till the end

I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout

How to make UITextView so high that it exactly fits the text content on iOS7?

I have a UITextView that's embedded into a UIScrollView. I would like the text view to not scroll and be exactly as high as required to show all of the text.
So the width is fixed and I set the content insets to indent the text a bit.
How do I get the correct height? I tried to set the frame's height to the content height but still it scrolls.
This should do the trick:
-(void)resizeTextView
{
CGRect frame = _textView.frame;
frame.size.height = [_textView sizeThatFits:CGSizeMake(frame.size.width, INFINITY)].height;
_textView.frame = frame;
}

Resized UIWebView returns to original size when scrolling inside a UIScrollView

I have a UIScrollView with certain details, labels, etc,on top, and a UIWebView with HTML content at the bottom. (I know I shouldn't put a UIWebView inside of a UIScrollView, but I need all the content to be scrollable, not just the webview).
My steps are:
1) I set the UIWebView scrollEnabled=NO, and calculate the UIWebView content size and update Its frame: (in webViewDidFinishLoad:(UIWebView *)webView)
CGRect WebViewFrame = webView.frame;
WebViewFrame.size.height = 1;
webView.frame = WebViewFrame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
WebViewFrame.size = fittingSize;
webView.frame = WebViewFrame;
2) I calculate all the UIScrollView's content and update it's contentSize:
CGFloat contentHeight = 0;
contentHeight += mainInfoView.frame.size.height;
contentHeight += .......
contentHeight += webView.frame.size.height;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, contentHeight)];
At first this appears to work perfectly, the UIWebView gets resized to the correct size, and the UIScrollView's contentSize seems to expand as expected.
THE PROBLEM is when I scroll or move the scroll view, the UIWebView instantly returns to its original size, and I think the UIScrollView contentSize also changes.
And I have no idea why.
Any help would be very appreciated!
Most likely you are using autolayout
Disable the autolayout property.
Reason why its happening : Autolayout will update the views to their original position using current constraints.

Width of multiline UILabel in UITableView keeps shrinking with autolayout

I have a UILabel functioning as a title in my UITableViewCell. It displays fine the first time it is used, but each time it is redisplayed the width shrinks. All my layout is done in IB, nothing in code at the moment.
[EDIT]
The left hand side of the label is pinned to the (barely visible) box that says "New Words". The right hand side is pinned to the superview. Height is set to 44px.
I am able to fix this by using the following code in layoutSubviews. I don't like it at all though since it involves setting the frame. I would like to understand the underlying problem.
[super layoutSubviews];
CGRect frame = self.titleLabel.frame;
CGSize size = self.titleLabel.intrinsicContentSize;
frame.size.width = size.width;
frame.size.height = 44;
self.titleLabel.frame = frame;
[super layoutSubviews];
Here are a few screenshots of the problem:
First time:
Second time:
Third+ time:

UITextView that expands dynamically to text inside a scroll view using auto layout

I have a UIScrollView that includes some components, and one of these components is a UITextView, what I want is to let the UITextView dynamically expands with the UIScrollView, in fact I use autoLayout, so this code is not working:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
300 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
What I want is to help me doing this with storyboard elements.
Instead of setting the frame, drag one of the height constraints for your text view into your controller to create an outlet. Then in viewWillLayoutSubviews grab the contentSize and set the constant of your text view height constraint. That should accomplish what you're doing with the frame in your code above. Just be sure that your text view has constraints from all edges to your scroll view so that the scroll view can size itself properly.
in my case, what I did is kept the scrollview as super view and added all constraints for the subviews and the uitextview has constraint from all edges to the scrollview. then updated the content size in -viewDidLayoutSubviews method. here is the code snippet:
-(void)viewDidLayoutSubviews{
NSDictionary *attributes = #{NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
CGRect rect = [_detailText.text boundingRectWithSize:CGSizeMake(_detailText.frame.size.width - 10.0, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attributes
context:nil];
CGRect frame = _detailText.frame;
frame.size.height = ceil(rect.size.height) + _detailText.textContainerInset.top + _detailText.textContainerInset.bottom;
_detailText.frame = frame;
_detailText.contentSize = CGSizeMake(_detailText.frame.size.width, _detailText.frame.size.height);
[_contentScrollView setContentSize:CGSizeMake(_contentScrollView.frame.size.width, _detailText.frame.origin.y + _detailText.frame.size.height)];
}

Resources