Scrollview with auto layout not adjusting height of scrollview - ios

I am developing small IOS application in which I am using scrollview with auto layout.Everything is working fine.But only thing when I hide any of the view inside the scrollview it is not adjusting scrollview height according to that. My view hierarchy looks like
Scrollview -> View -> view1
-> view2
-> view3
-> view4
So in above situation if I hide view3 then it is not adjusting layout.It left empty place in place of hide view. Am I doing something wrong? How to use auto layout and scrollview with dynamic height?

Hiding a view does not remove it from the view hierarchy, only makes it invisible.
If you want to do this with autolayout, you need to remove the view from the hierarchy (removeFromSuperview) and then adjust the various constraints as needed. This generally means you will need to maintain a reference to all constraints that will need to change.

you should use UITableView and use deleteRowsAtIndexPaths to remove view

You should change its width and height to 0. Animating this property results in a graceful decrement in the scrollview's content view. Make sure to properly set the autolayout for the subviews.

Related

Do I mandatory add views in UIScrollView in order to scroll those views?

I have used auto layout and after designing screen I need to scroll all the contents. If I add all the screen views inside a new scrollview, all my constraints will not work. So whats the easiest way doing this?
The Easy way is ,
Take One scrollview in Mainview then add another view(Contentview) in scrollview and then add all the content in content view. so, you can able to scroll contentview. and when your contentview will be scroll then all control will goes up and down. Due to contnent view.
Your hierarchy will be something like this ,
Mainview
Scrollview
ContentView
Textfield
button
Label
On below link , You will find best tutorial regarding
Set Constrain to Scrollview with Autolayout with Example
and last but not the lease ,
Don't forget to untick Adjust Scrollview insets in viewcontroller property.

iOS Scroll View doesn't scroll with auto layout with views added in programmatically

I have a ViewController with a scrollView and a content View inside.
Some labels and stuff inside the Content View as well.
Now after that, when the view load i have to pass in data from other places to this view controller, and then load some other views by code.
The scrollview Content height and height did get updated, and i did place the loading and adding of the views at ViewWillAppear. But even if the content size is bigger than the height of the scrollview, i just can't scroll it.
Scroll View Height:667. Scroll View Content Height :1632
ContentView Height :1632
Your scrollview did not scrolled because you gave fixed size to content view.
Don't worry, just make the outlet of height constraint.
Check below image , how to crete outlet of constrain,
Note :- You need to create out let of Height Constrain.
From your image.
And now set in your viewDidLayoutSubviews
-(void)viewDidLayoutSubviews
{
NSInteger screenheight=[ [ UIScreen mainScreen ] bounds ].size.height;
if (screenheight > 504 + 64)
{
_contentviewHeight.constant = screenheight-64;
}
}
And last but not the least , Don't forget to remove Tick from Adjust Scrollview insects,
In case of Autolayout, whatever view you are adding to scrollview. You have to add constraint (basically top constraint) so that Scrollview would calculate contentsize automatically.
refer below to add constraints programatically.
iOS create NSLayoutConstraint programmatically for table view cell content
Edit :-
Like you said SView is in storyboard. So create the constraint from storyboard itself on SView.
And E,R and H View will be added constraint programatically
And the link is just an example
You will use [self addConstraint] instead of [self.ContentView addConstraint]
Moreover refer this one too :-
iOS Autolayout Vertically equal space to fill parent view
OKAY, Finally.
After trying and retrying and to no avail.
I just gave up on putting auto layout on the ViewController which have the scroll view itself.
So I untick the auto layout, and then VOILA.
Apparently, the view controller which have the scroll view don't need autolayout. Just the views that i put in via code need auto layout in their storyboard. After i adjust all the constraints in the view properly and never giving them aspect ratio constraints but just fixed their width and height. Done.
So I hope anybody who have the same frustration as me see this and heed my advice. Don't bother putting auto layout on the scroll view or its content View. Just Untick it altogether. Do the autolayout for the views you want to put in. You just have to specify the views' width and height properly and it will work.

Using Auto layout with hidden views

I have a view controller with a UIScrollView. Inside that scrollview I have two UIViews. viewA sits onto of viewB. I am using Xcode 5 and auto layout in this project. What I would like to do is when viewA is hidden, move viewB to the top where viewA was sitting (technically, still sitting, just hidden).
I looked at this SO answer: How to use auto-layout to move other views when a view is hidden?
However it hasn't really helped me. I connected a IBOutlet which was a constant to the vertical spacing to the top of the scroll view and set it to 0.0f when viewA was hidden. However it makes no changes to my UI at all.
First get the Top Space to SuperView NSlayoutConstraints Outlets for both subViews and then do the following:-
[self.aView setHidden:YES];
if([self.aView isHidden])
{
self.bViewTopConstraint.constant = self.aViewTopConstraint.constant;
}
using this the second UiView will go to the place of first UIView.
For Scrollview you have to set the constraints value properly. No need to set any contentsize. Once you set the constriants scrollview will work automatically.
Check the attached screenshot.

UIScrollView Not Scrolling?

I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.):
UIScrollView doesn't use autolayout constraints.
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
Leave autolayout enabled.
Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
Understanding the contentOffset and contentInset properties of the UIScrollView class #blog
What's the UIScrollView contentInset property for? #Stackoverflow
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).

UIScrollview won't scroll when Autolayout is used

I have a scrollview which has two subviews (iPad). The view hierarchy is as follows:
-- UIScrollView
-- UIView1
-- UIView2
The frame of the UIScrollView is the size of the screen, the frame of UIView1 is also the size of the screen, but the frame of UIView2 is (0,0,768,2000).
The scrollview doesn't scroll vertically. According to the apple documentation, the scrollview should automatically set its content size. Can anyone help me out with this issue as why the content size is not being set properly ?
P:S: When I use a single view inside the scrollview and set a proper vertical constraint, it scrolls properly.
Scrollview won't scroll if you enable autolayouts. Technically when you scroll, all the elements in the scrollview change their position. So use auto layout if you are fixing the position of the elements in the scrollview.
Instead use a UIView as a container view inside a scrollview which contains all other objects like button , label, imageview etc. And then you will be able to scroll.
Check the below link for more details:
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
In your problem try to put proper values of content size. Also check if the vertical scrolling is enabled or not.
The question is old, but the correct answer would be to make sure the autolayout constraints are all set.
Vertically, you should have some constraint pinning your View1 to the Scroll View Top, another one pinning your View2 to the Scroll View Bottom, and one third one setting the vertical space between View1 and View2. Also check that the views themselves (View1 and View2) have constraints for their heights.
Horizontally, pin one of the views to the Scroll View in both leading and trailing space, and the other view left and right to the first one (so they will have equal widths).
Once all of those are set, the content should scroll correctly.
Use contentSize property of scroll view in code. The best way is to use 'viewDidLayoutSubviews' method
(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(568, 594);
}
2.Always make horizontal space >=0 between lowest of inner views and a scroll view. Automatically, auto layout make suggestions like '-160' and this negative space will not scroll.
If the Content size is more than the scroll view frame then only scroll view scrolls otherwise not .
Main Point : Mistake is in set 'contentSize' of 'UIScrollView', you need to set manually.
As seem in your Question
scroll view frame = screen frame,
view 1 frame = screen frame,
Its okay but
view 2 frame = (0,0,768,2000), then it overlapping on first view because your view2 x,y position is (0,0), so may be your first view will not display.
If you want to vertically add view 1 after view 2 then your view 2 frame should be
view2.frame = CGRectMake(0,view1.frame.origin.y + view1.frame.size.height , 768,2000);
And after added both of view then you need to manage contentSize of UIScrollView , in your case such like
scrollView.contentSize = CGSizeMake(768, view1.frame.origin.y + view1.frame.size.height + 2000)
I put simple logic, That may be helpful in your case:
How to enable scrollview scroll automatically when used with AutoLayout:
Please see the below link which help you to fix it correctly:
https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7
Question for above help guide:
Which view is parent view?
scrollView or parent of scrollView?
Answer:
- Parent Of ScrollView (Give equal with and Equal height to view which is parent of scrollview)

Resources