Autolayout not working properly - ios-autolayout

I have UIView in separate .xib file and I make UICollectionView in it.
To add collectionViewCell I made separate .xib file.
Now below collectionview I want to add UIView.but my problem is I set height of collectionview dynamically by using code.
But when I add this using autolayout constraint to add view below bottom of the collectionview but view will add on distance where It put on UI.
I want that first setup collectionview than view placed below it.
Problem screenshot 1
Below UIView constraint I have already set 2
Thanks
Nirav Zalavadia

Instead of adding the UIView below the UICollectionView, place the UIView in the bottom of the screen.
Then, use a constraint from the bottom of the UICollection view to the top of the UIView.

Related

How to add an existing view to a UIScrollView

I have created a view and the content is larger than the screen. How do I add this view as a subview of a scrollview? For some reason I can't do this in Document Outline. It will only let me add the scrollview as a subview of the existing view.
Select your view in Interfacebuilder and then use the menu command Editor:Embed In:Scroll View

Xcode add collection view cell to collection view on interface builder

I am relatively new to using XCode interface builder.
When trying to drag a collection view cell onto an empty collection view, the interface builder refuses to do so.
Please have a look at attached screenshot:
Am I doing anything wrong?
Note that I can do the same on a storyboard but not on a normal xib file.
You can only add a UICollectionViewCell inside a UICollectionView when working on a Storyboard file. For sure, you must be using a single nib file and that's why it isn't working.
If you wanna use a custom UICollectionViewCell, then you must create a subclass of UICollectionViewCell. When subclassing UICollectionViewCell don't forget to check the box that says "Also create XIB file" beneath the expandable menu of "Subclass of". Now, try to drag a UICollecionViewCell onto the canvas and custom it the way you want.
Another posibility would be using a Storyboard file. Try setting your UICollectionView inside a scene in the storyboard and then drag a UICollectionViewCell inside it. When dragged, create a UICollectionViewCell subclass and assign it to the UICollectionViewCell you dragged previously inside your CollectionView in the Identity inspector.
In either case, don't forget to set the reuse identifier of your UICollectionViewCell in the Attributes inspector :)
Here is a code sample from Apple of how to create a UICollectionView with a custom UICollectionViewCell using a storyboard.
https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860
Hope this helps :-)

What is the proper way to implement contentview with constraints in a scrollview in xcode using autolayout?

Recently I'm testing to do a very complicated view in xcode using nothing but scrollview and programatically added imgviews. Like this
What I did is, I put a UIView called contentview in a scrollview and I then add a lots of ImageViews in the contentview. The real thing is much much complicated than the illustration so I can't use tableview. Sorry for the poor illustration but I think you will get the idea.
Everything is fine until I tried to add the last constraint in code, which marked red in the picture. When I did it, it breaks the entire chain of constraint.
In another word, this is working
V:|-[view1(30)]-[view2(30)]-[view3(30)]
This is not
V:|-[view1(30)]-[view2(30)]-[view3(30)]-|
And btw, what is the proper way to do it anyway? I tried to add imageviews directly into the scrollview itself but I can't make the view to use autolayout like this
H:|-[view]-| The superview here is a scrollview
The result, this view will be very narrow in width. I guess scrollview doesn't have a width value or sth? that's my guessing. That's why I added a contentview as a holder for all the imageviews.
Try this, it will work. - http://natashatherobot.com/ios-autolayout-scrollview/
To summarize -
Add scroll view to main view.
Add a UIView (lets call it content view) to scroll view.
Now add all your views to the above UIView.
For each of them set the constraints so as the separation on all sides is 0. And finally add equal height and equal width constraints between the content view and the main view.
Ok, finally I did my own work.
The key to make scrollview and contentview and everythin else inside work automatically using nothing but autolayout is this.
1, Manually add scrollview to self.view, and a contentview to scrollview. Set both scrollview and contentview to setTranslatesAutoresizingMaskIntoConstraints(false)
2, set scrollview to H:|[scrollview]| and V:|[scrollview]|, set contentview to be H:|[contentview]| and V:|[contentview]|
3, Now try to add Views to content view and set their constraints to the contentview. You should see that when the views are larger than the current window size, the scrollview started to scroll. IE, this setting will automatically set the scrollview.contentSize using autolayout

Add ScrollBar to viewcontroller IOS 7 (Swift)

I have a view controller that contains various controls in swift, when it turns to landscape, it doesn't show all controls, how can i put a scrollbar please?
Add a UIScrollView to your UIViewController's view. Then add all of your subviews inside this UIScrollView. Adjust your scrollView's property accordingly when the device changes orientation.

How to add button,label and an image to multiple viewcontrollers using tabbarconrtoller

Im using a tabbarcontroller which leads to 5 viewcontrollers when each tabs selected.
I need to a add button,image and label which are common for all viewcontrollers.Is there any possible way to add these into tabbarconrtoller, so that i don't have to add these to each viewcontroller individually.
You can have a seperate view for your button, image and label, and then add it as a subview in all your view controllers.

Resources