Different view controllers for different screen sizes - ios

On my storyboard, I have created 2 view controllers; one for iPhone 5 and iPhone 6. How can I access different view controllers for different phone sizes. Online I found something that let me switch different storyboards but I only want to change one screen. Is there any way I will be able to do this or is making a duplicate storyboard with one different screen the way to go?

I find that size classes are so error prone that I was tempted to use that approach at some point (ended up making all my constraints totally independent of screen size and orientation but that's another, interesting but irrelevant, story).
One way you could do this is using custom segues with a naming convention. Let's say you already have a storyboard segue that links to a view controller for iPhone5 and you want to redirect it to your iPhone6 variant.
Give that segue a name that finishes with ".iPhone5". Then create a custom segue (dragging from the red square of the source viewController to the target one) and give it the same name but with a ".iPhone6" extension.
You can implement override shouldPerformSegueWithIdentifier in the calling controller and, if you're on an iPhone6, respond with false and programatically trigger (performSegueWithIdentifier) the iPhone6 segue using the identifier you received as parameter replacing ".iPhone5" with ".iPhone6".
You could centralize that code in a function or an extension to UIViewController to make it easier to implement on your various view controllers.

Related

Create view for iPad out of existing views suitable for iPhone

To put it simply, my app has two main views: the first serves to choose some values, the second displays a report. Both are TableView based.
The app looks well on iPhone with any screen size and for any orientation, but since the app’s thought to be universal, I want to get rid of those gaps between elements when the app runs on iPad.
I decided to have the app displaying both views on iPad screen simultaneously as official YouTube app does:
(1) and (2) are the first and the second View controller.
I see it this way:
1) Some initial view controller (additional navigation view controller?) checks idiom (iPhone / iPad) and chooses the next viewController. If idiom is iPhone, the first view controller is pushed.
2) If idiom is iPad, the «new» view controller is pushed. It has two view containers containing the fist view and the second view. Containers have autolayout constraints etc.
Questions:
1) Is it a right idea in context of Apple app design philosophy? What’s the better way to create iPad view combining existing ‘iPhone’ views?
2) Where to check for device type? Shall I use the second storyboard or something? Maybe there’s a good tutorial for this case, I didn’t find one.
Thanks in advance!
For the 2nd part of your question I think you want to look into
I think you want to look into Size Classes and Auto Layout
You can make a single view but depending on the size / orientation of the device - you can enable/disable certain components of the view.
You can pin views so that in portrait you have your 1st layout and in a landscape you have your 2nd layout.
With regards to your 1st question - you do have the ability to embed a view inside of another view
I'm not sure if this is what you are looking for - but as far as what apple is pushing I believe size classes is what they "suggest". You can do a lot. The only thing is make sure you turn on the assistant editor into storyboard preview mode it will help a lot.

Multiple States in Xcode 6 Storyboard. Animating/Moving items in run time

I have made various attempt to go storyboard only and limit my code when it comes to UI. Everytime I was getting stuck and reverting back to code. Since the release of XCode 6 and the new iPhone that have multiple screen sizes it make more sense that ever to go Storyboard only.
This time I am stuck on the following scenario. I want to create a custom search view controller that will have 2 states:
Search State. It will prompt the user for a keyword to search.
Result State. It will display the results to the user.
I am aware that this can be accomplished using the UISearchController, but the customer wants to customise the behaviour. Currently I have two view controllers and a push/show segue between them. I would like to replace that with one view controller and animate the display of the results.
Is there any way that storyboard can accomplish that. I am thinking of creating two view controllers (in storyboard) with different layouts. Both will be linked on the same class. I could create a segue between them, but then I will lose all the variable stored inside them and will not be able to animate between them.
On the transition between the two states some UI elements will be hidden and some others will be moved. I would like that to be animated.
I know how to do that in code without using storyboard, but then I will have to cover all different screen scenarios. I hope there is an alternative way.
You can do this with two view controllers, and set a segue between them. The trick is that the animation bit will have to be done in code, unless you write a custom UIStoryboardSegue to handle the animation.

Setting up two views, one an expanded version of the other, with one view controller

I'm trying to use Apple's Alternate Views sample code.
My app is going to add buttons to the view when rotated, like Apple's default Calculator app, so it's going to share a lot of the same code. In the example, they have one view controller and two UIView subclasses, LandscapeView and PortraitView, that they switch between on rotation.
Both views are going to have the same outputs, but landscape is going to have more actions. How and where would I put this kind of code? In the view controller or the separate view classes?
For example, would I declare the actions in the view controller, so that they set the outlet labels declared in the two views?
I'm wondering, with one view controller and two views, how to set up the variables and outlets that can be shared. In the sample code, it doesn't look like the landscape and portrait views inherit from the view controller, so I don't think that making the classes abstract would work.
Use the same ViewController and update your view programatically whenever orientation changes(make buttons visible or change their frames).
check out this answer to see how to get orientation changes;
https://stackoverflow.com/a/16959059/3343031

How to connect iPad and iPhone UI XIB elements to same UIViewController

this may be a noob question, but I'm new to iOS programming and I didn't find an answer to my question elsewhere...
Following issue: I am programming a universal app for iPad and iPhone using IB and storyboards. The app is already set up correctly and I have an iPhone and iPad storyboard in my project and both are connected to the right (same) view controller as owner.
So far, so good...
My iPhone app is close to completion and I now want to add the iPad UI, which, apart from the layout and maybe some rearrangement of buttons and views to make use of the larger display, will have the same elements and functionality.
Now here's my problem: when I DragDrop my iPad UI element (e.g. a UILabel) to the view controller to connect it I (obviously) can't use the same name...because the iPhone one is already there.
If the item is called 'myTextField' on iPhone I'll have to call it e.g. 'myTextFieldiPad' for the iPad, which means I'll have to branch out every time I want to access the text field depending on the platform. Analogous for IBActions.
In a nutshell: same view controller, two practically identical XIB files for iPad and iPhone with identical UI elements, how?
Am I overlooking something?
Help to point me in the right direction would be highly appreciated...
Beschi
Don't create new properties using drag and drop.
You can connect controls from different xibs to the same property on your view controller using drag and drop, instead.
What I mean is, create your properties using drag & drop from iPhone's xib, and then connect your iPad xib controls to the SAME properties you created from your iPhone's xib.
Try create two classes which are children of your BaseViewController. First controller has name iPhoneViewController: BaseViewController, second - iPadViewController: BaseViewController. In IB for first (iPhone) xib you need connect first class. For second - second class. After that where you create viewcontroller you need check idiome. For iPhone create object of iPhoneViewController and for iPad - second.

iPad split controller that doesn't hide the left pane in portrait

I am trying to implement a split view controller like UISplitViewController on the iPad, but I don't want the left pane to be hidden when the device is in portrait orientation.
So I've created a UIViewController subclass for this in IB and it works fine without any sub-view controllers. Now I'm trying to wrap my head around what is required to setup and manage the two UIViewController objects for the left and right panes. In my app, they are going to both be UINavigationController with a UITableView in them.
I've hit a mental road block about how to set this up and was hoping someone could point me to some sample code or give me a recommendation for architecture here...
The only reason to use the UISplitView controller is the show/hide logic it gets you for free. I would think it a lot easier to simply take the two view controllers (Root View & Detail View) and lay them on a standard UIViewController. You can then manage them more diorectly without overriding the intended behavior of the implemented controller.
THe settings app on the iPad does what you are looking for and I believe this is the approach that app takes.
Good Luck!
is setHidesMasterViewInPortrait still a private Api and the app will get rejected?
Create your UISplitViewController instance and then call:
[splitViewController setHidesMasterViewInPortrait:NO];
The compiler will give you a warning message but it will do what you want. You can get rid of the compiler warning by making a category on UISplitViewController that implements that method.

Resources