interface builder : make the view the same size as the iphone, to know exactly what constraints to add? - ios

is there a way to have the view from interface builder in Xcode, be the same size as the iphone we use to test the app? The size of the viewcontroller seems to be 600*600. So when I move a UILabel somewhere to the left for example, it will be placed as if it was on the right on my iphone. Which is not easy to set the right constraints.
Do you have some tips to manipulate the items and place them easily? For now, I just align the item with the center point, and move it little by little.

The reason this happens is because of Apple's emphasis of size classes. It looks like a rectangle because whenever you are adding constraints and views, they'll be applied to every size class (i.e. horizontal/compact, vertical/compact, etc).
In english, it means it's applying those changes to run as such on any device.
To create constraints and views for just one size, you'll want to edit the size class you are working with. At the bottom of Xcode where it says "wAny hAny", click that and you can set what device you want to work with:
The whole idea of this is adaptive design, and I've got a post explaining it in more detail here.

The thing is that there is no one correct size for the iphone screen because there are so many different screen sizes. You can set the view size to anything you want in interface builder by clicking on the view and going to simulated metrics -> size but what you really want is autolayout so that your views fit properly on every device.

Related

Prevent resize of UIViewController when changing orientation

The Maps app has a "custom" behaviour and doesn't resize the bottom menu when in landscape orientation.
What's the best way to achieve the same effect, short of resizing the view manually on every orientation change?
See attached images to get a better understanding.
This is done using size classes.
The easiest way to do it is to use the size classes right from your storyboard.
Right next to constraints and size related options from the size inspector you have this little + sign.
Hit it to define a different behavior for each size class.
See here for more, including how to use size classes from your code.

storyboard constraints (Xcode)

I'm new to programming and I'm trying to work with Xcode. I have no experience. I'm trying to make an app with Xcode. I'm done with the code, the only issue i have is with the storyboard constraints. I need some one who can help me to set them up.
So, i have 28 buttons as you can see in the picture, I want to keep the size of 65x65 pixels. that means that the distance between them should vary in different device sizes(4inch, 4.7inch and 5.5inch). i also should mention that i want it to be only in portrait view and only for iPhone.i hope someone can help because I'm to noob for this, I've searched a lot on the internet but I'm not able to do it.
UIStackView (iOS 9+)
UIStackView was introduced in iOS 9, which is very useful in your case.
Edit: Before getting started, make sure to have size classes to be w Any, h Any, this will make thing easier.
To use this, search UIStackView in your utility panel.
Drag an drop a Vertical Stack View on the view of your view controller and set the constraints so it spans the whole screen.
Select the stack view we just placed and set the distribution to Fill Equally.
Drag and drop a Horizontal Stack View onto the vertical one.
Select the horizontal one and set the distribution to Fill Equally.
Place your buttons in the Horizontal Stack View.
Copy and paste the horizontal stack view multiple times based on number of rows you want.
That's it.
You can control the buttons' background images to make it smaller (65px), or you can place a view as its' parent then align horizontally/vertically to place it to the center, like this:
Orientation
If you'd like to lock the orientation throughout the app, go to the settings of your project.
Sample project
I made a sample project for this question, you can check it out here: https://github.com/cyhsutw/UIStackView-example

How do I get a button to position on the bottom of a view controller in Xcode 7.2?

I used to be able to do this:
UIButton *bigBottomBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 60)];
I also used to be able to just drag a button onto a storyboard and add a constraint that would hold it to the bottom of the parent.
What is going on with Xcode, Autolayout and Apple for that matter....is my Xcode not working properly? Have I missed a major memo? is Apple just going downhill fast?
Your button-creating code used to work (and still does) if self.view's frame was correct at the time you created the button. Note that the view doesn't necessarily come out of the xib or storyboard with the correct frame; the xib/storyboard contains the view at some design size which might not match the current device. This wasn't as much of a problem when all iPhones had 3.5 inch screens, but became a pretty common problem with the advent of the iPhone 5's 4 inch screen.
The view isn't guaranteed to have its correct frame until its superview's layoutSubviews returns, so if for example you're creating bigBottomBtn in viewDidLoad, that's too early. Many questions on stackoverflow cover this problem. You either need to set the autoresizingMask of the button, or implement layoutSubviews or viewDidLayoutSubviews to update the button's frame, or turn off translatesAutoresizingMaskIntoConstraints and install constraints. Note too that your view can change size if you support rotation, slide over or split view multitasking, or if your view can be the detail view of a UISplitViewController, so it's a bad idea to try to guess the correct frame of the button based on the device's screen size at the moment the button is created.
Note that storyboards now by default use a design size of 600x600, which isn't the size of any device. This is probably because if Apple chose some device's size (say, the iPhone 5's 320x568) as the default, and you happened to use a device of that size as your primary (or only) test device, you could easily forget to think about what your app will look like at other sizes. However, you can explicitly set the design size to some device's size if you want:
I usually use “iPhone 3.5-inch” if I don't specifically need something bigger, because it lets me get the most scenes on the screen simultaneously (and produces the smallest screen shots for stackoverflow).
As for “I also used to be able to just drag a button onto a storyboard and add a constraint that would hold it to the bottom of the parent”, I have good news: you still can. Example:
However, you do need to be careful if you have filled your root view with a table view as appears to be the case in your screen shots. You need to drag the button to the document outline in that case, because if you drop it on the table view, Xcode will assume you want it to be the table view header:
Trying to pin a table view header to the bottom of the screen would be folly.
As for the Editor > Align menu, I have found that the items can be mysteriously inactive, which is frustrating.
Note, though, that only the “Horizontally in Container” and “Vertically in Container” will work (when they work at all) with a single view selected. To use the other items in the menu, you need to have at least two views selected, because the other items align the selected views with each other by setting their frames:
If you only have one view selected, Xcode doesn't know what other view you might want to align it to.
Those menu items are perhaps useful in the springs'n'struts model, but they don't add constraints, and under autolayout you probably want constraints to enforce the alignment at run time.
As far as I know, those menu items have never added constraints, but I'm not going to reinstall Xcode 6 to verify that, because there's a convenient popover that will add constraints corresponding to all of those menu items:
In xcode you always need to add buttons according to its visibility. As you said you need to show button on top of tableView and it should be aligned to bottom. For that You just need to arrange the order of items. as shown in the image below.Provide the layout for the button.

Different iOS devices size programming approach

I am starting a new project which should be working on every iOS device size possible.
The project is rather simple. The main view will be a scroll view and it will hold a '+' button (where it says 'button' in the image) in the top right corner. (It does not really matter, I'm just trying to give the general idea of what I'm trying to understand and implement.)
A small example:
What is the right approach for this kind of problem?
Should I create a different storyboard for each device?
Should I start creating an adjustable scroll view that will hold the needed buttons with some constraints (if at all possible)?
I have read this tutorial:
auto layout
which explains the auto layout nicely, but does not mention the issue I'm trying to figure out.
Any thoughts?
Use AutoLayout and position your views relative to one another (so no x pixels spacing between views). Make only one storyboard for all devices, when a different view should be loaded on another device (like a completely different layout), select the appropriate size class and adapt the views and constraints.
Then it will be recalculated on every device.
The storyboard has a preview section where you can choose a device to simulate the view on.

Xcode 6 Storyboard the wrong size?

Built a new project from scratch in Swift in Xcode 6 (Beta 1) and have seen some strange behaviour with Storyboards and the output that I am viewing.
I have built a simple interface (as shown below) - with properties of View Controller included.
When I run this in the simulator I would expect 'Hello, World' to be central in the user interface - however it seems that this 'Square' is simply being fit into the iPhone screen and thus the wrong view is being shown (see below).
My question is: has anyone else seen this behaviour and how did they fix it?
Thanks!
Do the following steps to resolve the issue
In Storyboard, select any view, then go to the File inspector. Uncheck the "Use Size Classes", you will ask to keep size class data for: iPhone/iPad. And then Click the "Disable Size Classes" button.
Doing this will make the storyboard's view size with selected device.
While Asif Bilal's answer is a simpler solution that doesn't involve Size Classes (which were introduced in iOS 8.) it is strongly recommended you to get used to size classes as they are the future, and you will eventually jump in anyway at some point."
You probably haven't added the layout constraints.
Select your label, tap the layout constraints button on the bottom:
On that menu add width and height (it should NOT be the same as mine) by checking their checkbox and click add constraints. Then Control-drag your label to your main view, and then when you de-click, you should have the options to center horizontally and vertically in container. Add both, and you should be set up.
If you are using Xcode 6 and designing for iOS 8, none of these solutions are correct. To get your iPhone-only views to be sized correctly, don't turn off size classes, don't turn off inferred metrics, and don't set constraints (yet). Instead, use the size class control, which is an easy to miss text button at the bottom of Interface Builder that initially reads "wAny hAny".
Click the button, and choose Compact Width, Regular Height. This resize your views and cover all iPhone portrait orientations.
Apple's docs here: https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/SelectingASizeClass.html or search on "Selecting a Size Class in Interface Builder"
In Storyboard, select your ViewController and go to Atribute Inspector. At the very top, under Simulated Metrics you have Size and Orientation properties which are set to Inferred. Change them to desired values.
In order for an application to display properly on another screen size, you also have to setup constraints, as described by Can Poyrazoğlu in the first post.
I had this issue in xcode 6 and there is a way to resolve the resize conflicts. If you select your view, at the bottom you will see an icon that looks like |-Δ-|. If you click on it, you're project will resize for different devices.
Go to Attributes Inspector(right top corner) In the Simulated Metrics, which has Size, Orientation, Status Bar, Top Bar, Bottom Bar properties. For SIZE, change Inferred --> Freeform.
On your storyboard page, go to File Inspector and uncheck 'Use Size Classes'. This should shrink your view controller to regular IPhone size you were familiar with. Note that using 'size classes' will let you design your project across many devices. Once you uncheck this the Xcode will give you a warning dialogue as follows. This should be self-explainatory.
"Disabling size classes will limit this document to storing data for a single device family. The data for the size class best representing the targeted device will be retained, and all other data will be removed. In addition, segues will be converted to their non-adaptive equivalents."
For anyone using XCode 7, it's very easy to design for a specific device size (instead of the default square-ish canvas).
In Interface Builder, select your ViewController or Scene from the left menu. Then under Show the Attributes Inspector, go to the Simulated Metrics, and pick the desired Size from the dropdown menu.
You shall probably use the "Resolve Auto Layout Issues" (bottom right - triangle icon in the storyboard view) to add/reset to suggested constraints (Xcode 6.0.1).

Resources