Using Auto layout with hidden views - ios

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.

Related

remove gap when a subview is deleted in scrollview

In Objective-C, I have a UIScrollView in which multiple subviews (created as xib) are placed. When I click the delete button of subview I want to delete this particular clicked subview alone and rearrange the scroll view without any gap of the deleted subview..
I have successfully removed the subview using the corresponding tag of subview, but I want to arrange the views in scrollview without the gap
How can I achieve this?
If you're using auto layout, you'll want to create variable references to your constraints of these subviews. Then when the delete action is performed, update the constraint values to move the other subviews into the correct placement.
You have 2 options to use achieve your goal.
1. Add UIView in UIScrollView Programmatically.
Code structure :-
Add a view1 on UIScrollView than increase content size of ScrollView according to view1 hight.
Remove a view1 from UIScrollView than decrease content size of ScrollView according to view1 hight.
2. Auto Layout.
According to me "2" option better approach. You can use prefer tutorial link for how to add a view on scrollview with Auto Layout. Using UIScrollView with Auto Layout in iOS

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.

Placing a UIView under a UIScrollView with autolayout

How can I display a UIView under a UIScrollview with autolayout ?
I have a scrollview and a UIView below it(the grey box in image below).
Adding it has a footer inside the scrollview is not what I want because as you can see I have tabs that can scroll horizontally... The same view needs to be under each tabs. I programatically add child view's to my scroll view.
Is it possible to put a UIView outside the uiscrollview ? If not, how can I add it above every child view at the bottom ?
I have tried messing around with the constraints but nothing worked. I am stuck.
Thanks in advance !
Storyboard :
ScrollView constraints :
UIView constraints :
I found the solution. You need to drag a UIView above the UIScrollView and add a space to the top and bottom layout. Also, I added a leading and trailing space constraint.

IOS9: UIScrollView offset in UINavigationController

This is beyond frustrating. I'm having an issue with the UINavigationController (and thus UINavigationBar) pushing my UIScrollView's content down at the top, similar to the issues found here:
iOS 7 -- navigationController is setting the contentInset and ContentOffset of my UIScrollView
IOS7 : UIScrollView offset in UINavigationController
I tried both the recommended solutions of either setting self.automaticallyAdjustsScrollViewInsets = true in my view controller's viewDidLoad() method and deselecting Adjust Scroll View Insets in the Interface Builder. Both solutions remove the margin at the top, but as a few others have noted, I can't scroll anymore.
The UIScrollView and ContentView fill the entire view controller. My content view (inside the UIScrollView) contains labels, segments, and a UITextView at the bottom. Does it have something to do with the content in Content View?
I can attach pictures if needed. Using XCode 7, Swift 2, and iOS 9.
I solved my issue. In auto layout, I wasn't setting my content view's height constraint.
Previously, I had pinned the bottom of the content view to the view controller that contained the UIScrollView.
I removed that pin and explicitly set the height. It works now.
I just ran into the same issue. I had an "About" view with a bunch of labels containing static text. I had a UIScrollView inside of a view controller and it was being pushed down. I had a label pinned to the bottom of the screen that was being drawn off page when it was not necessary to do that.
I had the following view hierarchy:
View Controller
View
Scroll View
Content View (UIView)
Label
Label
Content View was pinned to the bounds of Scroll View, but the width and height had equal constraints to View.
I resolved this by adding another UIView as a child of View. My hierarchy looks like this now:
View Controller
View
Inner View (UIView)
Scroll View
Content View (UIView)
Label
Label
I set the height constraint on Content View to be equal height to Inner View. This resolved the issue for me and everything started displaying correctly. Inner View is pinned to the borders of View.
NOTE: In the hierarchies, the Labels should be children of Content View. In the preview, it's showing that the Markdown lists can't go that deep.
May or may not apply to you, but if you have a Nav controller and you don't need it to be translucent you can do something along the lines self.navigationController.navigationBar.translucent = NO;
This may help with your odd offsets.

Scrollview with auto layout not adjusting height of scrollview

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.

Resources