View dimensions doesn't alter with iPhone dimension - ios

I have created one view, which I want to use for all iPhone dimensions. So I've set constraints in the view's subviews and its working fine. But when I tried to launch the app in a phone with dimensions less than that of the xib I created, the view projects outside the superview when adding as subview. So now each time when I am adding as subview I have to set the view's frame according to the device's screen dimensions.
I want to know whether there is any other way to resize the view's dimensions in accordance with the device's dimensions, other than setting the view's frame manually.

if you are using the the auto layout in your xib then go to
editor menu-->Resolve auto layout issues-->ALL views in view-->update constraints. if there is no constraints applied then you can go for add missing constraints and see the difference.

Related

UIScrollView in Xcode using Springs and Struts

I am managing an application in iOS using swift language and Spring and Struts for positioning. For some reasons I have to use UIScrollView in it. But I couldn't found it's correct way of implementing using "Springs and Struts" layout positioning technique.
I am setting the view height to "FreeForm" and then Content View height to 1000 px but all things are showing in the same screen size.
Using Struts and Springs is no longer recommend, you should be using AutoLayout.
However, if you must:
1) Disable / Uncheck "Use Auto Layout" in Files Inspector for the View Controller, this will also disable trait variations.
2) Change your Simulated Size to "Fixed" in the Size Inspector for the view controller. Ensure the Simulated Metrics for Size is set to "Inferred" in the Attributes inspector for the view controller.
3) Add views and set the autoresizing mask's "struts and springs" to tell the view how to grow and shrink within its superview.
4) set the view's desired frame rectangle to the position relative to it's super view.
You will have to set the scroll view's height manually, since there are no other views to constrain the frame and allow the autoresizing to kick in. To do this, set scrollView's content size after the view controller loads. see viewDidAppear(_:) Otherwise the layout engine cannot determine the vertical spacing between the top of the button to the top of its superview (the scrollView).
Again, this isn't advisable and you should consider migrating to AutoLayout.
The setting for "FreeForm" is only for interface builder, so you can have a complete view of all the content in your scroll view, but it won't reflect on your app.
The height of the scrollview depends on it's content, so you need to add elements from the top down so it gets bigger.

Constraints in ios

I am learning constraints and spent whole day applying them to the following screen.It is not getting displayed properly in landscape mode.Basically i am not getting how to fix vertical space between ,say, label-Welcome to BBBB and textfield-username so that textfield always appears below the label yet the spacing between them is adjusted according to the screens of different size. If i go for Pin\Vertical space, it automatically fixes the constant value.
Remove the label (or just move it out of the way).
Fill the space that you want to resize with a view.
Constrain this view to the objects above and below and to the sides of the parent view.
Put your label into this view and constrain it to the top of this view and centred to it.
You may need to change the constraints on the objects above and below it to stop them from changing height in an unwanted manner.
This new view should now resize as the device changes orientation but the label should remain at the top of it.
Repeat on other areas of your layout (i.e put things that are not moving around as you want them into sub views and constrain these views to resize accordingly). Experiment with using variable heights, fixed heigh constraints and 'equal heights with' constraints on the views that you add to get them to resize as you need.
Edit: but you have a lot of vertically stacked items in that view - you may never get them all to fit into a horizontal orientation. You may need to remove a few, or add a scroll view or force that view only to layout in portrait mode or... Don't forget to check that it works on all devices you are targeting.
#Ali Beadle was right. Since i had a lot of vertically stacked items, lining them up in landscape mode was not possible. So, i went for scrollview.
I have taken a ScrollView first and then a UIView named ContentView on that ScrollView. I have made the width of ContentView equal to the width of parent UIView and kept its height fixed to 568. In my code i set
_contentViewHeight.constant = self.view.frame.size.height;
Now it scrolls in landscape mode while in potrait mode, it does'nt scroll.
I run into Autolayout problems all the time. But I finally figured out a way to overcome a lot of issues that arise from it.
I simply will use a container-View like ScrollView or even a plain old UIView, place all my controls in it. thats it. this makes things a lot easier for autolayout to figure out the constraints. in your case you could just use a UIView.
-start off by removing all the constraints you have I would start by selecting each control in the XIB and see if it has width/height constraint, select it then press the delete key then at the bottom of the project explorer you'll see all the constraints that auto layout has select each one then delete. that should get rid of any complaints that auto-layout might have.
-Place a UIView object inside your main View then
-move all the controls inside it. Then
-add the necessary constraints for the container view so it'll resize in different orientations and don't worry about any constraints inside the container view (auto layout will figure them out automatically).
that does the trick for me usually.

How do I make a UICollectionView fill it's superview with autosizing?

I have a UICollectionView with a dynamic size (variable amounts of data get added after it is added to the view) within a container view and I can't figure out how to get it to fill it's superview using autoresizing.
I have tried every combination of autoresizing masks (via xib and code for sanity) and nothing has worked. I have also verified that it's superview has autosize subviews.
Edit: It's not sizing to it's content either, the current behavior is that it is just staying at the size specified in the xib.
Sounds like you might be confusing autoresizing masks with auto layout. Autoresizing masks tell a view how to update its frame when its superview's frame changes. It does not tell how to lay itself out initially. Try setting your collection view's size to the size of its content view.
xcode/iOS: Autoresize to fill a view - explicit frame size is essential?

Why is AutoLayout not taking care of my UICollectionView

I have built a very simple sample of an app (Source code on github) using a UICollectionView.
Everything is working fine, as long as the app is in portrait mode. When it is changed to landscape mode however, the content cell is not resized appropriately, and thus, nothing is displayed.
I thought that all the necessary AutoLayout constraints are in place. I am aware that I can implement collectionView:layout:sizeForItemAtIndexPath:, but my goal is to use AutoLayout as much as possible (simply to understand AutoLayout better).
What am I missing here?
You can use autolayout to set the position and size of the collection view. And you can use autolayout to set the position and size of the subviews inside each cell. But you cannot use autolayout to control the position and size of the cells. You must use the collection view's layout object to set the position and size of each collection view cell. If you want the cells to change size when the interface orientation changes, you must update your layout object to report the new size and invalidate the layout.

setAutoresizeMask does not work when Orientation changes on iPad

I have miserable and hard-to-belive problem:
I have View controller with self.view created in IB
I have added some controls on it
I have overrided shouldAutorotateToInterfaceOrientation: with return YES;
I have set autoresizeMask to one control for flexible height, expecting to be shrunk when changing to Lanscape mode.
However, it does't happen... Any ideas?
I'd recommend one of a view things:
Check your main view has its own resizing set to have flexible height.
Check that your view doesn't have autoresizing flexible top and bottom margins.
You say that this is a "control" - if it is a UIKit control, its probable that Apple doesn't allow changing it to have flexible heights. Take for example UISwitch. A flexible switch couldn't resize width or height as it is system drawn for its size. This may apply to any other UIKit-defined control where the autoresizing mask isn't appropriate to it's display.

Resources