Scrollview doesn't scroll with multiline label in it - ios

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.

It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.

I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

Related

Swift how can I get UIScrollView dynamic height

I googled but have not found working solution, I have UIScrollView adding inside UITextField and UIButtons with NSLayoutConstraint, I don't use storyboard and setting everything programmatically
my issue is: can't calculate dynamic height of UIScrollView.contentSize because I am using AutoLayout
also tryed UIScrollView.layoutIfNeeded() but no luck
can any one guide or advice me how to to get it work?
#DonMag's comment was right solution to my problem
stackoverflow.com/a/44933358/6257435
if you have in UIScrollView objects with AutoLayout don't forget add .bottomAnchor to last item
Sum of scrollview subview’s height and constraint constants is the final height. Even after constraints layed out, it’s always summ. So just calculate it, and you height. But why do you need calculating scrollview’s content height? If it’s adjusting wrong, set your subviews height constraints, top and bottom contraints to scrollview, and content size will be calculated automatically

UITextView not visible on UIScrollView

Ok, so what I have is a UIScrollView that is constrained to all four sides of the main view, centered both vertically and horizontally, and set to have equal width and height to the view. All of the subviews that I put on top of the UIScrollView are showing up when I run the app, exactly where I want them to be, but only the UITextView at the bottom is not. It seems like I've tried every combination of constraints but it never appears when I run the app regardless of what I do. Here is a screenshot of the constraints in the interface builder:
And even when I preview the file Main.storyboard before running it looks like this:
But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:
Any help with this problem will be greatly appreciated! Thanks in advance.
Avoid putting all your subviews directly inside the scrollView. The autolayout will break apart.
You need to :
Add a UIView inside the UIScrollView, with constraints 0-0-0-0 to leading-top-bottom-trailing to the UIScrollView, and put all your subViews inside that UIView.
After that, you need to set the contentSize of your UIScrollView by code.
Also, you can:
add missing constraints to see what is missing.
The left panel in UIStoryBoard (Document Outline), you can press the red arrow showing up to see what is missing or conflicting.
Check out Apple documentation for more details.
If you want your scrollView only scrolls vertically you shouldn't set its height equal to its superView so remove it and just set the width to its superView and then it should calculate the height based on the subViews inside it
I offer you to drag a UIView in your scrollView and set the constraints to its four sides, and name it containerView , then set its width equal to background view and start laying out your views inside it not inside the scrollview :)
If Height of all views in the scrollView is clear, it can infer the scrollView's height
in this case you can set a fixed height to your containerView like 800 to get rid of the red lines and check how it works :)
I have noticed some clues you might have to solve which could probably solve the issue. Since the scrollView is extend by its content's elements, you have to explicitly deal with each element in the scrollView:
For imageView on the top is not well constrained, you need to give it a width or aspect ratio. Fix the imageView issue might solve textview height problem, even it's not still is a good start.
TextView bottom anchor is equal to scrollView bottom anchor, but you have to know textView is also kind of scrollView. So it's not reasonable to constraint each other at same time. Because both of them don't have explicitly height. You can try to type some words in the textView which will at least give it height by its content, then the scrollView can detect the textView bottomAnchor. You might see something then.
Your scrollView's height is equal to view's height is also weird, scrollView shouldn't constraint its height at first. Because it can't be "Scroll"View anymore because it's height is constant. you should let its width equal to view, and let the height be decided by its element's height. Then it will be literally a scrollview.
Hope it helps

UITableView automatic dimension with UITextView max size of cell

I've successfully created UITableView with custom cells (automatically sized) that contain a label and UITextView - detailsTextView.
When scrolling of detailsTextView is disabled, the cells are properly resized according to textView's text.
I don't want, however, to have extremely large cells and set maximum height of a cell (and enable scrolling for detailsTextView when it reaches max cell height).
How can I achieve this?
When enabling scrolling for detailsTextView autoresizing of cells shrinks it to 0 height (overriden by min. height constraint in IB), but still it does not fill my planned maximum size of cell.
override func viewDidLoad() {
mainTableView.rowHeight = UITableViewAutomaticDimension
mainTableView.estimatedRowHeight = 200
self.mainTableView.register(UINib.init(nibName: EventCell.nibName,
bundle: Bundle.main),
forCellReuseIdentifier: EventCell.cellIdentifier)
...
}
The idea is simple. You need to keep track of UITextView's height change in textViewDidChange method.
Then if UITextView exceeds your maximum predefined height then you are gonna add a constraint which restrict the UITextView's height growth. Constraint should be something like setting minimum height of UITextView to your maximum predefined height and enable the scroll for UITextView.
If you use only auto layout, UITextView can define it's own size only if scrolling is disabled, unfortunately.
You should try to avoid recursive layout passes (e.g. when text view's height changes, then install more constraints, this will cause the height to change again, etc, etc).
What you can do is limit maximumNumberOfLines of your textView, and provide other ways to see the full text: as it was pointed out, it is a very bad UX practice to have a scroll view within another scroll view.
From a UI/UX standpoint I wouldn't do that what you are trying to do.
Having a Scrollview (UITableview) with another scrollview in it (UITextview) is bad. It can confuse the user because he could scroll the "textbox" on smaller devices instead of the "table".
However, you could go with this solution: UITextView change height instead of scroll
Another solution would be to check what happens when the cell with 0 height is shown. Check viewWillLayoutSubviews() and the constrains in IB.
Hope this gets you one step closer to the solution/answer.

How do I change cell height according to its content?

I have a Detail View Controller, which has different text lengths according to which cell you press in the Main View Controller, of which it segues to. How do I set the cell's height according to how much content is in it?
FYI, the cell consists of 3 labels and 1 image view. It is vertically set as UILabel, UIImageView, UILabel, UILabel.
I am using Swift.
This is what happens when I add the auto-layout constraints:
The way to do it is to use auto-layout. You need to set up vertical constraints from the top to the bottom of your cell:
Distance between the top edge of the cell and the top edge of the first label.
Distance from the bottom edge of the first label to the top edge of the image view.
Distance from the bottom edge of the image view to the top edge of the second label.
and so on ...
Auto-layout will take care of sizing the cell depending on the size of the content you put in your labels and the image view.
You should also remember to set the estimatedRowHeight property on your table view to some meaningful value. This will help the table view defer some of the calculations of the content size to when the user starts to scroll.
Also, set the rowHeight property on your table view to UITableViewAutomaticDimension.
There are 4 things you'll need to do:
Set your tableView:heightForRowAtIndexPath method to return UITableViewAutomaticDimension.
Set your tableView:estimatedHeightForRowAtIndexPath to return the average height of the cells. You can set it to something static like 100.
Set your Auto Layout constraints so that all subviews are tied to each other vertically, and the top-most and bottom-most views are tied up to the cell's contentView.
Set the label's preferredMaxLayoutWidth property either in code or in your Storyboard to be equal to the label's width.
To fix this, I added the sizeToFit() property to my label.

Detect a UIScrollView's height dynamically

I have a real problem with detecting UIScrollView height. I searched a lot and found this solution:
So I dragged from Content View to View and made Equal Height. But I does not work for me.
I also tried to calculate scrollView height according to elements height in it, but I do not think that it's a right way.
How can I set UIScrollView height dynamically? What is the correct way for doing it?
Your ScrollView should add a ContentView like this:
select your scrollView and in size Inspector bottom, set the width like this:
for more details on how to handle scrollView in Interface Builder your can check out this AppleDoc Working with Scroll Views
Hope this help you!
You only way to set height of your scrollView is by calculating the height of individual objects added to scrollview.
Programatically, calculate the height of each UI object and at the end set content height to scrollview.
[scView setContentSize:CGSizeMake(300, height)];
In above line, 'height' is nothing but a sum of all heights of all UI objects calculated.
By doing the below stuff's, I was able to implement UIScrollView with Automatic height based on the contents inside.
Add scrollview to the main view.
Add one ContentView inside this container scroll view.
Give proper constraints inside this content
view to the container scroll view as well as to its internal UI
Components. Please note that you should not give fixed height to the
content View. The constraints of the content view should grow
according to the UI Components inside
This might give you a layout
warning as 'Unambigous.... '. To fix this, give 'Equal Heights' constraint relation between content view and scroll view
Try the above steps, and this will solve your issue :)
Do you want to add item (UIView, UILabel...) in your UIScrollView by auto layout? If so, just put these in it and create correct constraints for them.
The key point is each item which in UIScrollView have top, leading, bottom and trailing side constraints to their superview. Most important is Make Sure that your UIScrollView has top, leading, bottom and trailing Descendant Constraints to its sub-items. It will calculate correct content height of scroll view for you.
In your solution, if your contentView has zero frame, it will not give you any height for scroll view.

Resources