ios 6 Storyboards and Custom Layouts based on orientation - iphone

Been trying to find some tips on suggested approach for this and not having much luck.
All I'm looking for is to know what is the best approach to handle custom layouts for portrait/landscape modes.
I've seen some posts say in the storyboard add 2 views to the same ViewController and show/hide based on orientation change while some people suggest to use a totally separate ViewController for each orientation.
Which of this is the preferred method. I'm just starting my application, So I'd rather go for the widely accepted method than have to deal with complications later on.
Apple documentation still keeps talking about nib files and not storyboards in this aspect, so not being of much help.
My main focus is performance (I'm fine with having to code stuff instead of depend on the graphical interface for it). separate ViewControllers seem to keep the code in a clean way however if that involves populating views / clearing them every time orientation changes, seems kind of expensive(not sure if it is relevant).
Also if each scene in the application has 2 layouts then managing them I'm not sure how much of a pain it's going to be when the application grows big.
Please point me in a suitable approach for my case,
am not concerned about backwards compatibility. Just worried about the latest Xcode and ios6 if it matters for the decision
Thanks

I would say this really depends on the level of customization of the UI in portrait vs landscape. A large number of implementations I have done can be handled by either autoresizing/autolayout when switching orientation, or simply moving the elements yourself when the UI is rotated and the callbacks are fired. Moving the elements around should not be an expensive operation at all as it is a very common occurrence (again this depends on the complexity of your UI though).

Related

Storyboards vs. doing it in code

I'm working in a company where we have several iOS developers and we use GIT to work in the same projects together. We never use Storyboards or .xib files in our development process as it's nearly impossible to merge them correctly.
With the introduction of the iOS7 I thought about the fundamental differences between Storyboards and coding all the UI "the old way", without the use of Interface Builder.
Is there anyone here who does just that? And most importantly, is there something you CAN do in Storyboards that you can't do in code in XCode 5?
I'm going to split your question into two: NIBs, and storyboards.
As far as NIBs are concerned, source control issues can be painful but manageable, mainly because you've typically got one NIB file per view controller. You could imagine a situation where you have two developers working on two different sections of your NIB powered app without any merging issues. Storyboards are different, since you have one single file that describes most - if not all - of the UI of your application. Clearly there is a far greater potential for conflict issues there.
NIBs can be extremely useful and time saving, if used correctly. Here's an example: the iPhoto App on iPad has a very complex UI. The vast majority of that UI is laid out programatically. However, the app also uses NIBs to load in graphical elements which are then laid out in code. This is how the brush panel works - all the brushes are created in a NIB. This means that Apple don't have to have dozens of identical image/image view alloc/init pieces of code. All the creation can happen in a NIB (this was discussed in some detail in a WWDC 2012 session on the iPhoto UI - it's well worth tracking down).
So NIBs - sometimes good, can save you a lot of time, and whilst there are merge issues they can in many cases be easily managed and handled.
Then we come to storyboards. Storyboards are interesting. On the one hand, they are extremely helpful and useful for straightforward apps and developers new to the platform. I've just converted a UINavigationController based app from NIBs to storyboards and found some significant time savings (particularly around table views, since with storyboards you can take advantage of prototype cells).
However, if you're working on a large scale project with several developers I'm not convinced storyboards are that beneficial. There are, as you say, big issues with merge conflicts, and unlike NIBs it's not easy to resolve them since that single storyboard file controls all of your app UI.
Here's what I'd suggest (and feel free to ignore me!) - if you're currently developing apps and doing your layout/UI entirely in code consider whether NIBs might save you time. They may well not - they're not for everybody - but it's well worth at least considering. You may be surprised at how many large apps actually use NIBs (iPhoto, as I mentioned, but also many built-in apps provided by Apple, as well as many popular third party apps by large teams). I probably wouldn't consider storyboards unless you were a sole developer working on an app with fairly straightforward navigation. That's not to do down storyboards in any way - I love using them - it's just they're not really suitable for collaboration.
Somebody posted this comment in reply to your question - I wanted to discuss it:
There is nothing you can do in storyboard and can't do in code. Objects, gesture recognizers, segues, even constraints - are all available for you to build programmatically
This is technically true, but in reality there are things in storyboards/NIBs that are much easier than code. A good example of this is auto layout. Whilst you can certainly manage your auto layout contraints entirely in code, the harsh reality is that the ASCII auto layout representation is much harder to work with than the visual representation you get in IB. This is especially true on XCode 5, where there are massive improvements to auto layout in IB (I can't detail it too much as it's still under NDA, but Apple publically talk a bit about the changes here).
For me the only big drawback of storyboards is the slow loading time and the usual lag that comes when navigating the storyboard. I'm not talking about 2-5 view controllers apps. I'm talking about 10 and more...
My personal preference is smaller storyboards if I really have to use them(UITableView prototype cells) or just plain xibs.
Doing it in just plain code is just a matter of .... do you have enough time on your hands? :) Usually you wont gain a lot from doing it this way.
You should consider these issues in your dessision:
Development time -
Obviously, working with the xcode UI designer is much more faster and easy to learn when creating new applications from scratch.
In the programmatically way, you will have to define in code each and every element property you'll want to set.
Working with storyboard will make the development process much faster.
Code reuse -
When working with storyboard you will have to bind UI elements to the controller with bullets that adds additional hidden code in the storyboard file. Same stubs are added when creating segues between controller.
This addition hidden code will make make it more difficult to reuse the controllers in other apps that you'll build.
If you are planning to make mass reuse of you controllers code, than creating the UI elements programmatically will be more suitable.
Source code integration -
Conflicts resolving is a common thing when several developers commits changes to the file.
Creating and changing UI elements with storyboard additional changes are added to the storyboard file that sometimes makes the conflicts resolve kind of tricky.
On the other hand, when changing UI elements programmatically only the changes that you'll make will be added the the controller file.

Modular Architecture for a Large Application With Best Practices and Usage of Storyboards and XIBs

We are a team, creating a very big application for iPad that serves as an eReader for unprivileged children. The app is built halfway and I thought we should take a step backwards and review the whole design of the application. The application we are building should be very, very compliant with the current software development architecture practices for iOS. I have the explanation along with the questions below:
The application right now, has about 50 views (and increasing) and most of the Top-Level views are in the Storyboard (a single storyboard, that is) and the others are in XIBs (for the sub-views, reusable item renderers etc).
Is this approach alright?
Should XIBs be completely omitted for the modern iOS applications?
Should the storyboard be right the way it is or should it be broken down into sub-storyboards? If they should be, how should the exact process of decomposing the storyboard be done? How would the modules be determined?
If you have some tips to manage the application in the right way or some rule of thumbs to assist in a streamlined and modular application, please mention it in your comments.
I'm sorry for the long text up there and I thank you for reading.
This is a bit of a subjective one, but in my opinion it is still valid, and desirable, to use Xibs alongside storyboards. The idea of storyboards is nice, but with the current implementation they are definitely lacking some functionality, the most glaring of which is view reuse. I have worked on a few large projects recently, and storyboards always start out nice, but sooner or later you find yourself copy and pasting table cells, and then controllers, and then you have to change things in multiple places with each update, which is totally unmaintainable. So yes, definitely stick with the Xib files for reusable views.
I am less keen on the idea of multiple storyboards however. While this is easy enough to achieve technically, for me it invalidates the purpose of storyboards entirely. Their only use is as a (relatively) clear overview of the flow of controllers through the application. They add a few small conveniences, but in general I find they often create more code than they save, and create some odd code patterns (eg prepareForSegue:, having to temporarily store ivars when you want to segue to a controller in code that needs properties setting). As you say, how do you split up the storyboards into sub-storyboards? I can only think that whatever partitioning scheme you choose would be somewhat arbitrary, and liable to change at a later date when the app structure changes, which would be no fun at all. Maybe if your application has distinct modes of operation it makes sense to split up the storyboard, but I don't think this would apply to most apps.
So, IMO, single storyboard but use Xibs for reusable views.

Clean ways of managing device rotation with iOS 4.x

I always get stuck with managing rotation on iOS application, there must be some kind of efficient way to do it but apparently I haven't heard of it yet. My interface is too complex to be parametrized in InterfaceBuilder so I tried doing all these different things:
Build two interfaces, one for portrait and one for landscape, but I found it awfully tiresome to devise some methods that enable one view controller to keep up with the other, so when the device is rotated the second view controller knows where to pick up the story.
Change my views' frames manually inside willRotateToInterfaceOrientation:, but in this scenario my whole interface turns into a bloody mess quite randomly (while sometimes it does the job alright...)
What do you think best practices are? Where might have I gone wrong? What did I miss? Thx!
I always go for the second option and it has never let me down. If you do it right, you will have always the expected result. What I think it's happening to your application (and you call it bloody mess quite randomly) its because your UIView's will probably still have some autoresize definitions on the Interface Builder. Besides removing all the autoresizes I also uncheck the "Autoresize subviews" checkmark from the parent UIViews.
Just playing devils advocate to JackyBoy's comment, I think it depends on the complexity of your view. In lots of cases I have found it simpler to just use a landscape UI. The benefit for me is the ease of visualization. You know (more or less) what you are going to get without as much trial and error and it's easy enough to pass whatever data is needed along (i find that easier then dealing with moving UI components around programmatically). That said, I don't know if there is really a best practice. It's a do what feels best to you kind of thing, I think. Though if I had to guess Apple's definition of what the best practice would be then it might be to use the two views.
Oh, I should also add that you can leverage the 'springs' and such for components in a nib that can sometimes be enough to handle the rotation as well

Pros and cons of using XIBs and doing views programmatically

I want to decide if it is better to use XIBs or to designs my views completely using code.
So far I have read that when you design your views on interface builder they are pre-built, so even if they use more memory the user feels everything is faster.
People say doing everything using code is harder but I find it to be just as easy, so I want to know if anyone has experienced some real speed gains when using nibs.
What have been your experiences, advice, etc?
Thanks!
You should be able to do both -- there are times when building a view programmatically is better/easier, and times when using a .xib is better/easier. Even if you only ever do things one way, you'll run into code that does it the other, and you'll need to be able to deal with that.
If you don't know how to use IB, then building your views in code is certainly easier. That is why you should learn to use IB. Once you understand IB, it's way, way faster to put together most of the view-based UI your app will likely need. IB helps you line things up, center objects, align base lines, connect controls to their targets and actions, etc. I think it's safe to say that everyone who uses IB effectively experiences "real speed gains when using nibs."
You should know how to use both. Performance differences between the two are negligible and should not be the reason that you choose one or the other.
Many people who are new to iOS development have the misconception that nibs (.xib files) are inferior to programmatically creating your UI and that if you use IB you're not a good iOS developer. That view is 100% wrong. IB is created by Apple and in use by Apple's developers to create their own Mac OS X and iOS apps. If IB (as a tool) is good enough to be used by some of the best developers in the world, it's probably good enough for most of us.
In practice I have found that a combination of the two usually fits the bill.
In my own apps I find that .xibs are great for laying out the basics of your views quickly and they allow you to iterate very quickly while giving you a preview of what your view will look like. It's also much easier to use auto layout in a .xib file.
Then when you need to do more advanced things like add fancy animations or move views around that is what IBOutlets are for. Anything that you put into a nib can be referenced through an IBOutlet. This allows you do then programmatically make your view come to life.
Lastly, you should fully understand what a nib (.xib) is doing automagically for you. You should understand what happens when a .xib's objects are unfrozen. There are many resources on the internet to understand .xib files better.
Also, learn how to use .xibs in an encapsulated way. For example, .xibs are crazy useful for things like prototype cells and they allow you to keep your code base modular (much more so than storyboards). Also, you will require less UI code in your view controllers.
Lastly, I always say that people should think of IB/.xibs like jQuery. It's going to save you a lot of time but the best developers still know how to do everything in javascript if they have to.
Good luck and have fun!
TL;DR version
Performance is not a consideration when deciding to use .xibs or not.
Use .xibs because they give you a preview of the view you are creating and they allow you to quickly iterate
In practice most apps will use a combination of both. You will programmatically add animations or move views around but the .xibs will be a starting point
Understand fully what happens when the objects in a .xib are unfrozen
You'll be more productive but be sure you fully understand what is happening behind the scenes.
I would always use XIB files unless there was a reason not to. This allows your views to be maintained easily in the future.
Some reasons for creating the views programmatically might be:
A control needs to be resized,
repositioned or otherwise altered
depending on something else
Controls
need to be added or removed
dynamically
There may be more reasons but there are not too many.
If you programmatically create views when there is no need you make it a lot more difficult for other developers to try to figure out what the view will look like and to change it.
If you build your views programmatically, you have control over the loading of elements. e.g. you could use lazy loading, and load secondary buttons, subviews, etc. a fraction of a second after the more important elements, allowing the key parts of the UI to come up faster. You could even animate some elements into position.
If you use IB, you get guides as to proper element spacings and positioning, but you could always copy the coordinates from IB into code if you aren't changing the design that often.
For simple UI elements, you will end up with more lines of code to maintain if you create them programatically.
IB and NIBs do a lot to optimise loading/unloading of views, but it is largely oriented to minimising memory usage vs. perceived speed for the user. For example, lazy loading if anything might make the app UI slightly slower, but it should make memory usage lower. This in turn could make overall app performance better on a large application, and is very much encouraged, but it's difficult to define "performance" in a narrow way. It's also difficult to say when you should or should not use IB - there will be some times you're much better off doing it in code.
One often overlooked element to the IB or not debate is development speed, especially if you have multiple developers. On a larger team/project you'll probably have some developer(s) who specialise more in the infrastructure, business logic etc. of the app and some developer(s) who specialise more in the UI. In this case, use of IB will make it easier for them to work independently, which should make overall development more efficient.
I view IB as a core part of the development platform for iOS development. It's not the right solution in every situation but not knowing how to use IB will be a real limiting factor.

NIB/XIB files with Cocoa programming - faster development time?

I have been programming with the iPhone SDK for some time now.
I have not been using Interface Builder. This scares me a little. I know that in effect I may be 'fighting the framework' but I do not see it that way.
I find it very easy to just instantiate my UITabBarController in my app delegate, instantiate a UINavigationController, then push and pop view controllers as I go.
Naturally I do not have an extensive knowledge of how to architect an app with XIB files because I have never done so, however I do know the general gist of it, having built some Mac apps in Cocoa using NIBs. So I am not completely ignorant.
My question is whether there is an increase in development time when choosing to lay out UITableViewControllers and UIViewControllers using XIBs rather than programmatically instantiating them and then setting up the ivars.
As I see it, both methods still require you to subclass the view controller for customization which will probably occur for the majority of your views. As well, there are still manual classes required for delegates, and the process of connecting outlets from within the XIB seems comparable to me from setting an ivar.
Or am I missing some other major point?
Thanks!
Code takes much longer to write to configure UIs than IB does.
Plus, you can hand off design to designers and let them tweak the UI.
In the end they both accomplish the same thing. You should use either one depending on the circumstances. Most of the time writing the code to create and position views, and especially maintaining it down the road, will take much longer than using IB. In a simple app for the iPhone though, this might not be true and you'd be just as well off creating everything in code. Basically, you should know how to do both, and pick the path that involves the clearest code and quickest development.
IB shines when you're using it to actually lay out views; even two or three views can be a real hassle to lay out and configure in code. I do tend to use it for tab bar and navigation controllers, and sometimes for subcontrollers (usually only if I think the user is very likely to use it), but that's more just because I'm already there so I find it convenient.
With this new version 3 OS they're announcing next week, I'm hoping Interface Builder gains some of the flexibility it has in Cocoa, where you can add palettes for your own classes and even build up complex non-view data structures (by using custom palettes). We'll have to see, though.
Don't worry too much, IMO Interface Builder is a little over-rated too.
It's definitely useful for getting things up and running quickly, or if you have an app with a lot of screens that are tedious to setup, but you're not missing much.
For the uses you outline just doing things in code is fine, and possibly even a little easier to understand.
Laying out views, or custom cells though... then you get into a ton of font/color/position setting that quickly explodes into a lot of code, hard to maintain and tweak. Much easier to adjust what you want in IB in those cases.