Few beginner questions regarding the GUI in IOS development via Swift? - ios

I am an enthusiastic beginner in iOS development and I have faced some questions that may occur to many other beginners as well. Please note that I am not asking for opinion or suggestion, instead need some clarification to use the best option that I am offered.
Speaking of designing the apps interface via Swift. We have three options which are : programmatically, StoryBoard or Xib file. The question is, can employ more than one of these three for the same project or even the same page(screen) in my app?
When I am using Storyboard I can drag and drop different objects and use them. How ever I either need to set my own constrains for every single one of them or I can select all and click the set required constrains and they will be all set. But how come although I've defined the constrains my view is not responsive on various devices? Based on my understanding the only way to get the responsive in all the iOS platforms to set the constrains programmatically! Feel free to correct me if I am wrong.
It may not be the best practice but I personally am more fond of back-end comparing to the front end. Is it possible to design all the back-end first and set all the constrains at the very end!?

1.Speaking of designing the apps interface via swift . We have three options which are pragmatically , Story Board or Xib file . The question is can employ more than one of these three for the same project or even the same page(screen) in my app ? ]
You can use storyboard for UI designing and also Xib's also you can use for example if you want design custom alertviews. This is just an example.
2.When I am using storyboard I can drag and drop different objects and use them . How ever I either need to set my own constrains for every single one of them or I can select all and click the set required constrains and they will be all set . But how come although I've defined the constrains my view is not responsive on various devices . (Based on my understanding the only way to get the responsive in all the IOS platforms to set the constrains pragmatically!! Feel free to correct me if I am wrong . )
First understand the Autolayouts in iOS. This is very powerful tool for iOS developers for responsive layout.
We can do Autolayout in either ways i.e., programmatically or Storyboard. But I can suggest for storyboard is better. In Some cases we can use programmatically.
Follow these links:
Autolayout for beginners
Introduction to Autolayout
3.It may not be the practice but I personally am more fond of back-end comparing to the front end . Is it possible to design all the back-end first and set all the constrains at the very end !?!
As per my view, First design responsive UI, Check in all devices. In latest Xcode, We can even check UI in all screens.
Dont use multiple storyboards for every iOS device. Use Autolayouts for desgin UI with single screen in storyboard

Related

Is it a bad thing to create views programmatically? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So I'm new to iOS development and I found it easier to write the views all programmatically. So my views have UIViews, ScrollViews, UIButton, UILabel all created and positioned programmatically. (So I never used AutoLayouts).
I've now pretty much finished my app and want to make the iPad views, and realized maybe it was a bad idea to do it like this.
Is this bad practice or should I really be using auto layouts as much as I can?
If it's ok to do it how I'm doing it right now, what is the correct way to add different views for iOS and iPad? I've seen this answer below on how to find the device, is a simple if else statement sufficient? - iOS: How to determine the current iPhone/device model in Swift?
I am using programatic views in a live app and its awesome. A bunch of people I know us this as well.
Here is a little algorithm I use to choose between the two methods:
Are you building a fast app for a client or a hobby? Use storyboard with autolayout.
Are you building an open source project that will be used by many people? Use programatic UI
Are you building an app for the long run? (1+ year) Use programatic UI
Its also harder to make an app thats supposed to be rotated without autolayout. Because doing that with code takes much more work than autolayout. Most good apps dont use this feature anyways so I don't see much problem.
A good tip is never to use constants while writing programatic UI.
If you are going to make a button thats 100px in width, do not type in 100px anywhere in the code. Instead figure out the screen sizes and place the main views according to screen sizes. Then place the subviews or secondary views according to the position of the main views. If you do this correctly you will have more powerful multidevice layout support than autolayout.
Here is a little library I wrote, please inspect and play with the code on how I place the view: https://github.com/goktugyil/CozyLoadingActivity/blob/master/CozyLoadingActivity.swift
Also here is a good article I like about this:
http://www.toptal.com/ios/ios-user-interfaces-storyboards-vs-nibs-vs-custom-code
This is only fine, If you have enough time, patience, good skill on calculations and relationship configurations between different UI elements.
However, using Auto Layout is pretty useful and low time consuming than manual calculations.
We can easily create a dynamic and versatile interface that responds appropriately to changes in screen size, device orientation, and localization with minimal effort.
Read Adopting Auto Layout,to implement the Auto Layout in your existing application
TL;DR
It depends
Longer version
Clearly one size does not fit all. AutoLayout is pretty powerful both in Interface Builder and code (be it visual language format, or simple constraint setting), but sometimes it just seems like you're "rubbing your right ear with your left hand" - that's when adding views programmatically comes in. But beware of not having it different in each view controller - you don't want to introduce too much complexity in your project, right?
I personally like using AutoLayout as much as I can and whenever I can't use it anymore, or the StoryBoard View would get too messed up with millions of constraints, I try to separate views into containers - have the container be resized by AutoLayout and have the subviews handled by code.
Example would be a custom Media Player - maybe I want to have two stripes below and above the video - I could have the video and two UIView extended stripes handled by the AutoLayout. But the subviews (controls) in the stripes themselves would be added by code. It gives me the control over my code but still it doesn't introduce too much complexity.
First of all - if you want to develop for iOS you have to learn Autolayout. There are already a lot of different devices with different resolutions and maybe will be even more in future.
Secondary - if you want to work effectively with IB you have to read guide / watch tutorial videos and have some practice. It could be difficult to start but then you will realize that IB is powerful, fast and often the best way to develop GUI. Often, but not always!
Code advantages:
Easy to copy-paste and reuse GUI. It could be critical if you have
several similar views or want to reuse some old code.
Easy to resolve merge conflicts and check commits.
Easier to make styles - like the same font for all labels depending on the country.
More powerful (there are things that could not be done with IB) so you have to use it sometimes.
IB advantages:
You can see your GUI during development for different resolutions/localizations so you do not have to compile and run a project on different devices/simulators to check is GUI ok or not. Also IB will show you warnings if you forget some Autolayout constraints or there are conflicts. Saves a lot of time if you have a complex GUI with non-trivial Autolayout constraints.
It is much easier to understand someones else code if it is developed in IB. Especially important for complex GUI - not so easy to find a required label or button in a few hundreds lines of code.
Small bonus - if you want to use a custom control developed via code you can make it IBInspectable and use it without problems in IB
Just to summarize - if you do not need IB benefits (for example GUI is quite simple and does not use Autolayout) developing GUI via code could be easier and faster. But in case you have to support different resolutions and/or you have hundreds lines of GUI code in each view controller I strongly recommend to try IB.

iOS Layout - Whats the best solution?

For an iOS app i am currently working on, I need to manage the controls displayed on the screen based on the type of device the app is running on.
I will try to explain with a theoretical example (the actual numbers used below are not important, what interests me is best method to achieve the desired result).
Example:
In the case of the app in the screenshots, the button that overlaps the UIImageView should not be displayed at all OR displayed in another place on the screen.
So far I've worked with autolayout , but , from what i could figure, there's no way to say to xcode something like: hey, for a class size "Compact Width / Compact Height" , I want you to hide these buttons...but show them for "Regular Width / Compact Height" .
I also did some googling and saw some people talk about using different storyboards based on the device; I am also thinking, I could add/remove buttons dynamically based on the device type , but I think it's not so pleasant to have to add all of the constraints by hand (programatically that is).
So to resume, I would appreciate a suggestion of a 'best' way to do this , best meaning a combination of 'not so hard' + ' not so long'. Also, some code example (or links) would be highly appreciated.
Thank you all in advance !
I think it all depends how sophisticated it gets what you are doing overall. When you look at developer's code for large app projects very little is actually done in interface builder since the apps are so dynamic it's just too much work in IB.
Using multiple storyboards 'sounds' like a good idea, but often isn't. If you want to update/change one, in most cases you then have to change the other ones as well.
What I think is handy:
Use one storyboard (or multiple but for different parts of you app and not parallel ones) and then put in very 'special' cases multiple ViewControllers of the same class for the different size into the single storyboard. That way you won't forget about updating the second (and third) because they are all right there.
I would only use multiple 'parallel' storyboards for apps that support iPhone and iPad and it makes sense to really split them up..
Also:
I would as fas as possible still use just one representation and derive the other 'versions' from that in code. If you build a structured user interface you can then reuse (or just inherit) the code that 'hides' parts of the first viewController in the next. That way your code AND your IB files won't get cluttered.
Also think about internationalisation (if that is part of your future plans), since multiple IB documents and multiple instances of the same viewController layout really don't help for that ;)

Why Use Storyboards for Upcoming iPhone 6's Larger Screen? Difference?

I've read around that Apple is hinting to developers at a larger-screen iPhone by pushing developers to use Storyboard and auto-layout. I understand why auto-layout would be useful, to organise items according to screen size, but what do Storyboards have to do with anything?
I may be missing an obvious advantage (in relation to larger screens), so any help would be much appreciated. :)
A. You say that there are some notice from Apple about the larger screen of the iPhone6: This is impossible because Apple doesn't release any information until the date of Keynotes and WWDC.
B. You can or not use Storyboard, and you can or not use AutoLayout: the 2 things are separate because you can also use AutoLayout in nib or programmatically.
C. What developers don't know, is that Storyboard are just a market move. I spoken directly with a my friend engineer in Apple about this and the information is real. The reason is also obvious. Is not the topic to speak about this but you can search on google and you will find a lot of information about the advantages to don't use storyboard.
Yes, storyboard are easy, but are not developer friendly if you think to work in a serious project with a big team. In a team you can use GIT, you can use shared repositories, you can export a part of code with the interface...in all this thing, Storyboard are BAD!
So i encourage developers to DON'T USE storyboard and use at max NIB file, or write the UI directly by code.
There currently doesn't appear to be any advantage to using storyboards versus XIBs or programmatic layout as far as multiple screen sizes are concerned. Auto-layout is definitely advantageous as it allows you to define your interface elements in relation to each other (and the screen), as opposed to using fixed numerical values for location and size. This would allow the UI to then easily adapt to varying screen sizes and aspect ratios.
The only potential reason storyboards could become useful for multiple screen sizes in the future is if Apple adds functionality to them to allow you to define entirely separate views based on device/screen size. So for example, they might make it easy to show a view with an extra sidebar on a larger screen, but show a separate view without the sidebar on a smaller screen (they might make it possible to configure this easily in a storyboard, versus writing a bunch of code to detect the screen size and load the appropriate view programmatically). As of now, though, no such functionality exists, but that could be a potential source of the rumors regarding storyboards being useful for multiple screen sizes.

iOS 7 Transition - XCode 5 - Interface Builder - Three20

I've currently an App (with a lot of View Controllers, ...) what uses the "discontinued" Facebook three20 Library and any storyboard, xib file.
Now I need to update this App, the Deployment Target will be iOS 7 and I have to build it with XCode 5.
So my questions:
Is using the Storyboard a good Idea? Would you suggest me to recreate all View Controllers in the Interface Builder?
--
Another question: when adding a Label Programmatically using iOS 7 feature
self.edgesForExtendedLayout = UIRectEdgeAll;
how do I set the Frame for this Element? Need I determine if I am in Landscape, Portrait and Add the 20+44 for example manually to the y-Axis to start under the NavBar or are there better ways to solve this problem?
Storyboards are good, and you should absolutely use them in any future app you make. Whether or not you should rebuild a current app to use storyboards is a question of how big the app currently is, how much bigger it might get, how much longer you'll support it, etc.
If the app is already pretty massive and it won't necessarily grow much at all from here, it's probably not worth the effort to convert over to storyboards.
If the app is only a handful of view controllers, but you have big future plans for it, I'd take the time to convert it over to storyboards so that all the future development will go faster/easier.
I don't deny storyboards but I respect the custom view approach. It means I am creating my each & every component programmatically via code, be it a UILabel or a UITableView. Aligning code with ios7 specific requirements can also be handelled & controlled at granular level using custom View approach.
To your questions about setting frames for the element or detecting device orientation can very well be detected & adopted by writing code.
You can search more help as the information is available all around on programming aspects of iOS.

iOS Storyboard designed apps

Ok,
I'm very confused about something... being new to development on iOS I'm often pushed towards the storyboard/graphical design (specially to make file owner, segue, etc type of connections). HOWEVER; every time there's something besides a basic (dare I say primitive) design, I have to do things via code... problem is that just because you can do something via code doesn't make it right according to Apple. so you have to know all the rules to have you app approved. Even some of the examples from Apple (UISPLITVIEW with multiple view controllers -> MultipleDetailViews) do not have story boards just XIB... is that normal???
So question is: why have graphical design if you have to move towards coded solutions anyway. just to say that you have an easy to program interface?
can anyone help me understand that??
cheers!!
Three things (at least!) are going on here:
Storyboards are relatively new and a lot of examples existed before storyboards were introduced.
People are always dreaming up things that are more complex than a very high-level approach can handle. (Even with .xib files, which are more flexible than storyboards, people write custom code to get around limitations.)
For examples, code is linear and a bit easier to follow than the description of a bunch of clicks and drags.
So, first of all, a storyboard is nothing more than a conglomerate of .xib files. When you use storyboards, Xcode handles unloading all the nibs for you. At its core, your app still uses these nib files.
Additionally, your app won't get rejected if you use code to set a property instead of setting it in Interface Builder, or vice versa. I don't know that I would say Apple's stance is just because you can do something in code doesn't make it right. I would argue that they urge developers to set as many properties via IB because less code that you write means less bugs that you introduce. If I have a view in a nib, and I check its "hidden" property in IB, that's no different than me saying view.hidden = YES in code. Apple doesn't prefer one over the other. But using IB relieves me from having to know that the property to hide a view is "hidden" and that it accepts a BOOL as an argument. If in iOS6 they decide to make it shouldHideSelf, using IB's hidden property will be automatically updated, where as I would have to manually update my programmatic setting of the hidden property.

Resources