Auto resize UITextView in UIScrollView - ios

I have a question, how can I resize a textview in a scrollview and scrollview also must resize to be conformable with what size the textview will be.
Thank you.

Without auto layout it is much easier. Just get the contentSize property of the UITextView:
float textViewHeight = textView.contentSize.height;
And then set scroll view content size according to the content size of the text view:
scrollview.contentSize = CGSizeMake(320,textViewHeight + 100);
This will make the height of the scroll view the height of the content size of the textview plus 100 pixels (in case you have other stuff in your scroll view).

Related

how to resize scroll view after remove subview swift?

I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size

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

UIScrollView ContentSize Limited to the Size of the UIScollView

I have set the contentSize of the UIScrollView as follows:
self.scrollView.contentSize = CGSizeMake(320,800);
But as you can see in the image below it still displays one last line under the UITabbar. Why?
I am using iOS 7.
The scrollView's contentSize refers to the size of the content that can be scrolled through. For example you could have a contentSize height of 2000 px, because you have 2000 px worth of content to scroll through.
Your contentSize is not the problem; you indeed want it to be 800 (I'm assuming that's the height of all your text and graphics, not including the tab bar at the bottom.) What you want to change is the actual size of the scrollView object itself. Then the scrollView will be the correct height (something like scrnHeight - tabBarHeight) but the contentSize will still reflect the size of the content (the stuff inside the scrollView).
[self.scrollView setFrame:CGRectMake(self.scrollView.frame.origin.x,
self.scrollView.frame.origin.Y, 320, scrnHeight - tabBarHeight)];
self.scrollView.contentSize = CGSizeMake(320, 800);
And then you should be all set. Now it's possible that 800 isn't actually the number you want; the number you want is the exact height from the top of the scrollview to the very botom of that text at the bottom. But this is the idea :)

Dynamically layout scrollview contentSize with AutoLayout

I have a simple case:
I have the following views hierarchy:
View
ScrollView
View
Label
My label is positioned at the bottom of it's super view, and it's height is changed dynamically, depending ot the size of the text that it is rendering.
My aim is to adjust the label size depending on the text, so that no text is truncated, and with the growth of the lines of the label, to grow the scrollview's content size, so that the label always is positioned at the bottom.
How can I do that with autolayouts, preferably from IB only?
If you want the UILabel to stick at the bottom of its parent UIView using autolayout, then that view won't expand its height when the UILabel height increase, what really happens is that the UILabel will move up to occupy more area.
I f you wish your UIView to expand, then don't use autolayout in that UIView, and position your UILabel at a constant origin, then change the view's height & the scrollview height as per the UILabel text.
you can get the UILabel size using the below line of code:
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];

UIScrollView contentSize height equal its frame height, why there is still a scroll bar?

I config scrollView contentSize same as its frame height(I also test <), Why scorll bar still shown, it seems that the scrollview content size is not what I configed in code.
I think you are place scroll view in view with size for Retina 4 inch, and scroll view automaticaly resized.
add line in viewDidLoad method
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
or set up right autoresizing mask in IB.

Resources