I have read a lot of SO question about this already but nothing seems to work in my case. Simply when you add a UITableView and you test it and rotate the screen it will keep the width of the portrait orientation.
How can I make it so that it will always fill the screen's width for any screen size?
EDIT : My size inspector
Use the Autosizing feature in Storyboard for your UITableView. How to set it:
1.Select your UITableView.
2.Open the right pane -> Size Inspector:
3.Scroll down to the View section, here you'll see the Autosizing menu:
Don;t forget to disable Autolayout feature:
You're using AutoLayout in your XIB, so the AutoResizingMask stuff isn't your best route to success. You need to set the autolayout constraints on your TableView so that there is a 0 px gap between the leading (left) and trailing (right) sides of the TableView and its superview.
You can see from your screenshot that you already have some constraints in place, but they're probably not correct. I suggest you start by resetting all the existing constraints: I find that's usually best when I've got confused about where I am with constraints.
Select the top level view in your XIB file
Select Editor -> Resolve Auto Layout Issues -> Clear All Constraints in View from the menu
Select your TableView
Select Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints from the menu
If that does the trick for you, I'd advise that you spend some time getting familiar with AutoLayout and review the constraints generated: you can select and edit them in the size inspector. This is also a pretty good tutorial.
For custom views, I often add my constraints programmatically as it's really easy to keep track of them - they're much more visible than when they're built in IB. I use a set of categories, provided by another SO user, that you'll find here and they make the code very easy to read and maintain.
Related
I was trying to practice Auto Layout in iOS, and I started with very simple UI. Please see image for understanding my problem.
All the text files are in middle of screen (I have deliberately kept on guide line), still you can see in preview, controls are not fully shown. I have not chosen specific size. Size is 'Inferred' still I am not able to see all the controls on UI.
I tried both adding and removing Auto Layout, but no luck. What should I do to create generic UI which will work with all the sizes of iPhone and iPad.
This image is without use of Auto Layout.
After enable autolayout and size classes you have to apply autolayout constraints.
Autolayout is a detail topic. Few basic things when applying autolayout is:
UI element need four constraints.
position x
Position y
height
width
So you will select first label (Number 1). Then press control and drag to superview. You will be provide options. Select Leading space (This will handle x position)
This is the way you can press control and drag:
http://www.appcoda.com/wp-content/uploads/2014/07/auto-layout-login-trailing.gif
Go to size inspector. You can see the constraint.
Press edit and change its value to 25(for test).
similarly control and drag again to superview and select Top space. (This will set y position for label)
This is simple way for the above taken from AppCoda
http://www.appcoda.com/wp-content/uploads/2014/07/auto-layout-control-drag.gif
You can change the value of these constraints according to your need.
UILabel and uitextfield get width and height from their content size. So don't need width and height constraints.
Now when you preview on any device this label will be stick on top left side of screen.
So this is a complete mechanism. You have to apply constraint to every ui element.
Below is a link to very comprehensive tutorial by
http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2
At start this tutorial tried to create three views using autolayout. At the end it shows very similar scenario like yours by applying constraints to button and labels.
The problem here is that your constraints are not set correctly to work with every size of iPhone and iPad. You are setting the leading edge constraint to be a fixed size from your view controller's view to the subviews.
The simplest way to solve this issue would be to have a container view that you center in the view controller's view and then use constraints to set 'Center X Alignment Constraint' and 'Center Y Alignment Constraint' to set the container view's center to that of the view controller and then add your subviews to the container view.
As a side note auto layout has a reputation of being hard to learn, you have to put the time in to learn it, I would start with Apple's Auto Layout Guide.
I created a UICollectionView in the storyboard editor and added it to my (custom) ViewController. Like every view controller in storyboard, it says the size is 600x600 and so the UICollectionView, which takes up the whole view, is also 600x600.
This is not correct though, as I am writing an iPhone app and so the real dimensions should be 320x568!
Because of this, when I add items to my collection, they are placed off the right side of the screen. For example, I first add a cell with an image in of size 160x213. It is left justified and it takes up exactly the left half of the screen. When I add the next image, there is a huge gap and it appears on the left side, partly cut off. The third image I would expect to appear below the first, but it doesn't appear at all. I believe it is off the right side of the screen. This implies that the size of the UICollectionView is 600x600 and not 320x568.
I should mention that I've tried everything I could think of to fix this. For example:
I tried adjusting the size of the collection view:
self.photoCollection.frame = CGRectMake(0, 0, 320, 568);
I tried unchecking "Use Size Classes" in the storyboard editor.
It seems to work if I uncheck "Use Auto Layout" but I would like to use auto layout. How do I get this work?
You can set the "Simulated Metrics" of the view in your storyboard like this below. Personally, I prefer using 3.5/4-inch to construct my layout, as with auto layout, i only need to add some constraints to elements and iOS will automatically support the 4.7 and 5 inch screen size.
I think you should google for some "Auto Layout" tutorials, and I don't think its too hard for you to pick up :D. For instance, if you want to set the collection view frame equal to your view frame. You can do it like this
Feel free to ask any follow up question if your have any, Cheers!
Everytime I run my iOS application, everything is off-centered. In this screenshot, you can see that the text seems a little off, and the black box is definitely not vertically centered along the page, although the text and box are centered according to the storyboard.
simulator screenshot
storyboard screenshot
I am guessing that the issue must have something to do with the size settings on the bottom of the storyboard. I am using wCompact and hAny.
Any help would be appreciated so that all this will look centered properly.
This is probably because you don't have your constraints set. You can click on the UIImage or UILabel and go to Editor -> Resolve Auto Layout Issues -> Add Missing Constraints (For All Views) if you want to use Auto Layout. Otherwise, look up some constraint tutorials and you'll have to set them manually. It's not too bad once you get the hang out it!
After adding my launch image using a xib (and I even tried the traditional launch image way as well) some of my view controllers have a strange right margin now. For example before I might have had a label whose width was the entire screen, and used auto layouts such that it was leading/trailing to the content margins, and now despite that after adding a launch image there is a margin (it's not on every screen, but definitely some). On one of my screens where this is most prevalent i'm using autolayout but overriding with constraints that I add programmatically. Not sure if that's related.
Has anyone experienced anything like this or does anyone know if adding launch screens affect the story board / view controllers in any way?
Thanks!
On the launch screen select your image view, then select the pin at the bottom right of xcode (3rd option). Make sure all constraints are set to 0, while making sure the red I is selected for each one when they are set and uncheck constraint to margins. Finally click add constraints :)
Hope this helps you!
Rachel
I haven't done iOS development in a while (since Xcode 4 and iOS 5). And I have a very noobish question:
How can I keep buttons at the bottom of a storyboard viewcontroller on both 3.5inch and 4 inch devices? Previously I could use 'origin' or something similar in the right side panel.
A quick solution, especially if you don't need to deal with rotation of elements much is to add some basic auto layout constraints to your view.
Once you have your button set in place in your view where you want it to stay in the view, select your View in the Storyboard and then from the Menu Select Editor -> Resolve Auto Layout Constraints -> Add Missing Constraints in View.
Simply doing that will give you what you want. Make sure you have auto layout enabled for your storyboard.
set autoresizing mask as shown in image to keep button at buttom in both 3.5 and 4.
i think you are using autolayout so apply first approach
with AutoLayout. add constrains for your widgets
without AutoLayout