iOS: dynamic height UIScrollview autolayout - ios

I making a article detail view like Facebook.
This view have content(profile image, writer, date, content text) and comment write view.
So Top view is UIScrollview, Bottom view is UIView.
UIScrollView have UIImageview, UILabel*3,
UIView have UITextView, UIButton.
When touch UITextView in UIView, the keyboard comes up.
So UIScrollview will be smaller and UIView position will be moved.
And User will write text, UITextView line will be added so UIView Size will be extended.
Eventually UIScrollview and UIView height not Determinate.
How to set autolayout this view?

Instead of having UIView for textView and button, what i would suggest you is, just add one scrollView set its constraints as below
Then add a UIView as container view inside scroll view, (select scroll view go to Editor, select Embed In, and select View) and set its constraint as below
Now add your imageView, labels, textView and button one by one, and make sure your set imageView's top space to view and button's (which is at the bottom of screen) bottom space to view, and all other view's top space and bottom space to their nearest views, this way scroll view can easily determine the height required for it.
One last thing, when user click inside textView you might need to move scrollView upward in order to make textView visible to user while he is typing, to do this one easy solution is just download the library 'TPKeyboardAvoiding' from here add all classes to your project and once done, set your scrollView's custom class to 'TPKeyboardAvoidingScrollView'. Clean and run the project, it should work..

May try it:
What I say below is about constraint:
UIScrollView top, left, right equal self.view top, left, right
UIScrollView bottom equal UIView bottom
UIView left, right, bottom equal self.view left, right, bottom UIView height equal 200(you set it)
OK, when you UITextView is touched, set the UIView height constraint to what you want(maybe 300)
I think it works!

Related

UIScrollView does not fill whole screen

I have a UIScrollView which is set up with AutoLayout. I have a constraint on the "Save" button from the bottom of the button to the bottom of the scrollview, set to 0. The scrollview also has a bottom constraint of zero to the superview. Meaning, that the Save button SHOULD be 0 pixels away from the bottom, and even says so on the attributes inspector (see screenshot). How come, when I run the project, or even view from the storyboard, that this button is NOT at the bottom of the screen? Why is the save button refusing to align on the bottom of the scrollview and thus the bottom of the screen?
According to this article: https://www.natashatherobot.com/ios-autolayout-scrollview/ , the best way to deal with scrollview is to make sure only one view (we will call this contentView) is within the scrollview. Place all the subviews within contentView, and the trick is to use equal height and width from the scrollview to that contentView. I was able to solve my issue that way.

Can't build UIScrollView with Autolayout properly

Can't build content inside scroll view with autolayout. Yes, I've investigated a lot, read apple technical note and some articles, I know, that content view should be constrained with scroll view's superview but can't still achieve the desired result. I expect this result:
fixed or scrollable (for small screens) portrait mode
always scrollable landscape mode, not to loose or overlap my fixed content
my button (red view) should always be at the bottom
my photo (yellow view) should always be fixed size and not to loose content
(like in my wrong landscape mode)
Making this work is actually pretty easy. All you have to do are the following steps:
Add the UIScrollView as subview of your UIViewController`
Add the UIView as a subview of the UIScrollView
Add the UIButton as a subview of the UIViewController. Do not add it as a subview of the UIScrollView if you want it to stick to the bottom.
Then add the following constraints:
Pin the UIScrollView to the top, left and right of you UIViewControllers view
Pin the UIButtonto the left, right and bottom of your UIViewControllers view
Pin the UIButton top to the UIScrollView bottom
Pin the UIImageView top, horizontalCenter and bottom to the UIScrollView
Set the UIImageView height and width constraints
Set the UIButton height constraint
And that's it. Do not add anything to the UIScrollView contentView and do not set the contentSize programmatically! The contentSize ist set automatically by Auto Layout.
Here are screenshots of the constraints and the result:

UIScrollView has an unexplainable position in UITabBarController with autolayout

Using Xcode to create a new project, a Tabbed Application.
On the first view controller I add a UIScrollView, bind it to the view for all borders. Then I add a UIView "Content View" with equal width as the superview of the UIScrollView. I add a label on the top and a label on the bottom of the content view, and make sure that the size of the content view amounts to a higher height than the height of the scroll view, so that we get a scroll.
Still I get a horizontal scroll bar in the view. Why? The width of the content view is bound to the width of the superview of the scroll view, which should thus be as wide as the device, and give no scroll. What am I missing?
The project is downloadable from here if you want to play with it.
Your problem is that you have two constraints for the ScrollView which are set relative to the margin.
If you make the leading and trailing constraints relative to the superview with no margin then your code works as you require with no horizontal scroll bar.

Using autolayout constraints to define height minus a constant with storyboard

Is it possible to have a View with autolayout , such that the subview has equal height with parent minus a constant?
The Hierarchy is UIView > UIScrollVIew > UITableView. The view is also embedded in a UINavigationController
Yes. You simply pin the subview to the top and bottom of its super view with the constant set to the offset you wish.
As the outside view changes, the inner view will change to match and maintain the offset. You will need to set left and right constraints as well using the same mechanism.
Edit after discussion:
The UIScrollView needs to know the size of your content. You can not simply use auto layout without a few adjustments.
Assumption is your view layout is:
UIViewController -> UIScrollView -> UIView -> UITableView
For your UIView, pin the top, bottom, left and right to the UIScrollView.
To see things properly in storyboard you also seem to have to center the UIView horizontally and vertically. Once you add this you should see the content fill the scroll view bar any edge offsets you set in your pinning.
Pin your UITable to the UIView top, bottom, left and right.
You are going to have to adjust the width in your code somehow to the size you want to scroll horizontally.
In Interface Builder:
Clear constraints on your subview
Select your subview
Choose Pin in right bottom corner of IB view
Select bottom pin and enter your constant (I entered 30), select top pin and enter 0.
Select other constraints if needed.
Accept with button Add x constraints

With Storyboards only, how do I create a UITableViewCell that can scroll left and right using UIScrollView?

Similar to Mail.app where you can scroll the cells side to side, I want to be able to do it in my app using Storyboards and UIScrollView with my UITableViewCell.
I add some buttons to the contentView of my cell, and then UIScrollView with an embedded UIView on top of that to contain the main portion of the cell. I then add a label to that.
I understand that scroll views in Storyboard work by specifying constraints to show what the contentSize of the scroll view should be.
So I position the UIView in the UIScrollView to be the same width and height of the scroll view, but give it a trailing constraint of say 50pt to allow the scroll view to scroll. When I run it I can then move the cell to the left to reveal the buttons.
But to get it work in the other direction I have no idea. If I add a leading constraint it actually moves the cell to begin with away from the left, offsetting the cell to begin with when it should be left to the user to scroll it. It works fine other than initially being pushed over.
I created a sample project showing it here: http://cl.ly/2i3m1W2T0i3C
Like I said I just can't seem to figure out how to get it to scroll in both directions.
Try setting the contentView of the scrollView with width higher than the scrollView itself.
Something like:
Assuming your scrollView.frame is (0,0,100,100)
[scrollView setContentSize:CGSizeMake(300,100)];
place your table view, let a part of your view out of the screen and check that case:

Resources