5 ms·
Using code for layout has several advantages: - easier version control and code merging - you don't need Interface Builder to review the layout code - code i
by _stephan 14y ago
Using code for layout has several advantages:
- easier version control and code merging
- you don't need Interface Builder to review the layout code
- code is easier to search, e.g. for the use of certain controls (Xcode doesn't support searching XIB files)
- code can be parameterized (you can e.g. use global constants for font sizes, colors and margins)
- layouts that follows certain rules (fixed heights and margins etc) are sometimes easier to build with code,
especially when the number of visible controls is variable or some controls are only optionally displayed
- you can easily refactor aspects of the layout into reusable components (i.e. functions or classes)
- cageface 14y agoI'm a big fan of declarative layouts and vastly prefer doing Android UIs to iOS UIs for this reason. But that's only because Android has a sane & powerful layout language. Obj-C is so hideously inflexible and verbose for this kind of thing that I prefer to stick to xibs despite their limitations. Of course there will always be cases in iOS apps where layout in code makes more sense though.
- bjustin 14y agoAutoLayout (new in iOS 6 and OS X 10.7) is decidedly OK to use in code or in IB.
- bennyg 14y agoI'm of the "do both" camp. Personally, I like starting out the interface in .xib because of the wysiwyg style - I've been an Illustrator and Photoshop design guy for much longer than I've worn my iOS Dev Hat. By laying out areas of apps in subviews of the main view, then manipulating those in code, I get very easy content sizing (3.5" vs 4" screen) without using the springs/struts and the terrible iOS6 autosizing constraints, and animations couldn't be easier. Just looking through my open projects right now, all of my ViewControllers have a buildUI method right under ViewDidLoad.
- cageface 14y agoGlad to hear I'm not the only one that thinks that the new iOS 6 auto layout stuff is a train wreck. The WWDC sessions on the subject are like Onion parodies of normal tech talks.
- mikec3k 14y agoI'm starting to prefer using code for layout, since it does make some things easier. When I inherit a large project, it's nice to be able to search & easily change stuff in code rather than having to tweak lots of xibs.