2 views for the same ViewController - iphone

I'm making an iPhone app using Xcode 7 beta 3. The app includes several ViewControllers. For my main page, i would like to have 2 different views: 1 for Landscape, and 1 for Portrait. Can it be done using swift ? (without using Auto Layout)

Yes you can use different size classes for each orientation and design your views accordingly. Note that if you do it without autolayout though then you will have to do this for every view size combination and orientation which gets extremely tedious.

Related

swift check if device is iphone 5 or 6 or ipad

Is there a function to check what device the user is using?
In one of my View Controllers I have a view inside a scrollview. In that view I have some text inputs. Then I change the height position on that view in order to prevent the keyboard from being on top of the view/inputs.
I don't need to move the view as much on an iPhone 6 compared to an iPhone 5.
print(UIDevice.currentDevice().name)
Will get you want you want.

Problems when trying to support 3.5 and 4 inch screen of iPhone

I started the development of a new app on the iPhone 5 and now I also have to add support for 3.5 inch screens. As you know, there is more space for content on a 4 screen than on a 3.5 screen.
When I run the app on a 3.5 screen, the whole layout is messed up. Is it possible to use the layout normally on a 4 inch screen, but place the layout within a scroll view when run on a 3.5 screen?
My idea was to scroll the page rather than completely redesigning it. How can I achieve that?
(or maybe it is somehow possible to design a layout for the 3.5 screen and the 4 screen seperately)?
It is totally possible to design completely different layouts for 3.5 and 4 inch screens. You would just need to add this :
#define IS_IPHONE_5 (((double)[[UIScreen mainScreen] bounds].size.height) == ((double)568))
And build separate cases, using if (IS_IPHONE_5) {do this...} else {do that...}
BUT
This should be done only as a last resort, because it would require a lot of manual/extra coding. You should ideally create the view in such a way that it automatically adjusts itself for both the screens. And you can do that in 2 ways :
1)With Auto-Layout i.e applying constraints on and between each view element. (Personally I don't like it a lot)
2)Without AutoLayout, by applying auto-resizing masks on each of the view elements. (and checking by toggling the form factor button in storyboard)
For that u need to add image of size 640* 1136 with name Default-568h#2x.png and addded to the image folder and then add the image in the Target-General- AppIcons
It very simple and it worked for me
I wouldn't suggest ScrollView since, if you go by that Method then there will be a lot of reframing of the UI ELEments
If You contain few elements then I would suggest go by AutoLayout or AutoResizing Mask, set your constraints in such a manner that they expand and contract depending on your views height,
or
If your View contains a lot of UI ELements that the constraints conflict then and only then go For Two XIb files in your App corresponding to the same class (set your ClassName to the Two Xins in identity Inspector), If you are using Story board then just Duplcate your Current View and Adjust it for iPhone 5 and set the StoryBoard Identifier different for each, and Then based on the Device height load the Views Respectively such as
if([[UIScreen mainSceen]bounds].size.height>480){
//Load iPhone 5 View
}else{
//Load iPhone 4 view
}

iOS views presentation

Im new with iphone programming and im facing issues with the views.
I can not respent the view properly, for example in this case. I have an ViewController with a tabbar at the bottom. This controller have other 4 controllers where i show each of them when a user clicks on the icons.
But when a new view from one of the 4 controllers appears on the main controller, i get a line under the view. In the picture is a purple one.
When i change orientation, this empty line appears in the same place. Some times, when i enter with landscape orientation in the app, this line appears at the left.
I'm working here with IB. so, can someone point me here to the right direction? :S
On shouldAutorotateTo.. method i have YES, do i have to manage the views and change size and place every time the method is executed?
do i have to create 2 NIB for each controller, being one for each orientation option??
edit: i changed the picture to be more clear and show other "bug".
On 1 i enter to the view on portrait orientation. change it and still having a wear line up stares. All my nibs are on portrait orientation and 320 and 480 size.
On 2 i enter to the view on landscape but the nib loads as portrait. When i change orientation, it does not expand at it should.
How do i fix this 2nd issue?
i don't fill like having 2 nibs per controller :S
Thx in advance !
Short answer for Your autorotation problem:
You can either create two nibs (and switch between them, when autorotation occurs), or use just one nib. This depends on your goal. If you want to create a totally different view (like music app shows cover flow on landscape, and song on portrait) you should create two nibs.
If your view is more like the message app you could easily use just one nib. Take a look at the "Size Inspector" (5th tab in Xcode 4), the red lines guide you. The apple documentation for Xcode 4 interface builder is a good start to get you into that behavior.
That space in your view looks like the exact height of the status bar in iOS, review your NIBs in IB to see if you have a simulated status bar for your view controllers. If you do, turn it off and you will likely see that your view is not sized right. Being that it's height is 460 rather than 480.

iPhone orientation management : what is the most efficient way to do?

I need to develop an iPad application which should manage the two orientation mode (landscape and portrait).
According the official Apple iOS documentation, there are 2 ways to proceed.
-The first one consists in adjusting views element when the rotation event is received. The main advantage is that we had only one ViewController
-The second one consists in displaying a specific ViewController for each orientation. Therefore, we have 2 ViewControllers.
The second approach seems to be nice, but I'am afraid by the numbers of ViewController that will be needed. What's more, the "data synchronisation logic" in the ViewControllers will have to be duplicated (or isolated from the ViewController) to be used in both orientation.
The application I need to develop will contain many "full custom elements" and many ViewControllers.
If anyone has advices or experience feedback, it would be really appreciated ;)
Thank's for reading !
The second way should rather be: using 2 different views (one for portrait, one for landscape) and swapping the view controller's view in willRotateToInterfaceOrientation:. No need to duplicate your data logic.
Which way to use? I would say: it depends.
If the lanscape and the portrait modes differ only by the position / size of views, I use the first one (plus you'll get nice animations of the frame changes)
If landscape and portrait are too different, I prefer the second one.
I usually solve this by taking advantage of the autoresizing techniques in the view combined with the implementation of willAutorotateToInterfaceOrientation and willAnimateRotationToInterfaceOrientation methods in the view controller.
With autoresizing techniques you can easily resize standard UI elements provided by Apple. If your UI elements doesn't have an impossible layout, you can apply the autoresizing techniques to them too. You must set the autoresizesSubviews property to YES in the parent view controller and select an autoresizing behaviour for each subview. For example, if you want it to resize to the right maintaining the view centered, you can apply the autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin mask in the subview.
If the autoresizing techniques doesn't do the trick, then you will need to resize each conflicting view separately by implementing the - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration in your view controller. If you have "full custom elements", you will need to resize them this way.
In my particular experience, I prefer to have only one view controller and one view for all orientations and manage them with these two techniques.
Hope this helps you!

How to make a universal app

I have a iPhone app I want to convert to an universal app so it can run on iPad. I have tried to find information about how to do it, but I don't find anything that answers my question. I have upgraded the iphone target in xcode to ipad. Then I get a new MainWindow-iPad.xib.
But I have many viewcontrollers and I have made those with code, not xib's. Do I need to have a if-clause in the loadView method that loads different viewcontroller depending on if the user has a iPhone or iPad?
It kind of depends on how you've implemented your loadView methods and how you want your iPad version to differ from your iPhone app.
If you just want your app to use the full screen, you can mostly just check the size of the screen and size your views appropriately and use the autoresizing mask to deal with things when you change orientations.
There are a few gotcha's with this approach, though. For example, modal views can work differently (not taking up the whole screen) and, obviously, the keyboard is a different size.
If you want to use completely different views for your iPad app then, yes, you'll quite possibly have if statements that tell it to use completely different view controllers.
Yes you have to create all xib's for iPad and then you have to write if-clause for Ipad xib to be loaded.