How to test behavior of Auto Layout constraints in xib without launching app? - ios

I'd would like to see how the controls on my xib behave without actually launching the app.
In the Xcode documentation it says "[t]o test the behavior of Auto Layout constraints when you are editing a nib file, hold down the Command key and resize the window."
Does this only apply to OS X or is there a way to do it with an iOS app as well?

In xcode 5 you can preview how your view will look in different device screen and OS's. Open the assistant editor (Command+alt+enter) set it to preview and select the viewController you want to test. In the preview tab you can change screen size and iOS version.

What I have found to work is to switch your view controller under Properties -> Simulated Metrics -> Size to Freeform.
Your view should now be resizable with the mouse and the constraints will be in effect during resizing.

If you are designing full screen view, you cannot scale it but you may apply retina 3.5-inch form factor to view it in different height.
If you are putting views in container like UIScrollView, you may rescale the container to see if subviews moving or resizing as designed.
It is powerful when you are designing stand-alone xib for cell or custom views.

Related

how to use a xib file for all device sizes in xcode

I would really appreciate if someone can tell me about how can I use one xib file to target different device sizes. I am creating a Custom Keyboard extension using a xib file having view property of "freedom". The view loads great in iPhone 4,4s,5 and 5s. However whenever i run the app for iPhone6 or 6s there is a blank space at the right hand side. (please see the attached screenshot, missing area is shown in red), looks like the keyboard doesn't stretch according to the screen size.
How can i fix that? I disabled the auto layout and use size classed option. I am a very new developer and coding using swift.
Thanks a lot
I would use autolayout for this, I would not disable it in the xib. Its also the reason as to why you have a space at the end of your view, as it is not expanding automatically to the sides, it has been set statically e.g. setting the frame.
I would have a look at this link but in the mean time I would re-enable autolayout then with your keyboard view container ctrl click drag to your main view and select Leading(left), Trailing(right), Top & Bottom Space to Container. Below is the popup menu from ctrl clicking then select the triangle at the bottom right then Update Frames, this will relayout the view. It might not layout the keyboard keys correctly but its is a start.

Storyboard UIViewControllers are too big for iPhone screen sizes

Whenever I create a new project and select the single view application, the UIViewControllers are too big for any iphone emulator. How can the view controllers be resized to properly fit onto the screen? and how do I begin to make the interface be consistent across multiple devices with different resolutions?
Go to Attributes Inspector -> Select a View Controller -> In the simulated metrics ,change the size.
You can choose the size you need.
You talking about Size Classes technology, when you create universal layout for all kind of devices. You can switch between exact models of devices to adjust UIViewController size that you see.
However, if you wish to change the size manually, switch to Size Inspector and set Simulated Size to Freeform and enter values manually
You need to use the auto-layout and constraints to make the objects on the View Controller sit correctly

Default size classes seem to only work for iPad (XCode 6)

I am trying to wrap my head around the new size classes in iOS8 and XCode6. I am attempting to create a nib, without a storyboard, and do something really simple--center a UIView on the screen.
Starting with a nib in the default size: w:Regular h:Regular, I place a 200x200 UIView onto the parent view and center it, then add contraints to pin it. In my preview pane, the UIView only shows up on the iPad view.
I need for this view to show up on iPads in lanscape and iPhones in portrait. I suppose some day i will understand why Apple thinks this is so much easier, but at the moment I'm bewildered.
When I switch my design view to any of the iPhone supported modes, the UIView disappears.
Even when I unpin the UIView and move it around in the design view, it never shows up in the iPhone view. In other words, I've tried everything to get this view to appear on an iPhone and nothing works.
Here is my UIView and settings:
Obviously there is something I'm missing, but I'm getting really frustrated trying to figure it out. Can anyone offer a clue? Thanks!
It's because you defined your view only for the size classes Regular & Regular. For them to show up on the iPhone you will have to configure the view also for the appropriate size classes, which in case of the iPhone is Regular & Compact where (<height> & <width>).
UPDATE:
The views generally only show up in the size classes for which they are configured. You currently have Regular & Regular selected, this can be seen from your screenshot. This means you defined it only for the devices with size classes Regular & Regular, which is the iPad in both orientations.
You need to add the views also in the size class that you are targeting, which are the size classes of the iPhone.

UIViewController, Auto Layout and iPhone 6

We are updating our Auto Layout-using app to natively support iPhone 6 resolution. We added the iPhone 6 launch image, and most of the views scaled nicely.
However, we are experiencing a small annoyance: when loading new view controllers, the views have the iPhone 5 resolution on the UIViewController's viewDidLoad method. The view only gets the iPhone 6 resolution when you hit viewWillAppear.
Why is this? Is there anything we can configure or modify to make the views have the final resolution on viewDidLoad already?
I think the right place to get geometry data from your views is in the method:
- (void)viewDidLayoutSubviews;
This is called when auto layout finishes laying the views. Please beware this method can be called multiple times, i.e. after a rotation of the device.
EDIT
In interface builder, there is an option to set the size for the simulated metrics. I'm not sure if this has to do only with what interface builder displays, or also with the frame of the view at load time. Maybe you can change it and give it a try.

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