Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

What is best Practices to use AutoLayout?

Since iOS7, I have been searching for standard GUI design pattern.

In my previous two apps, I unchecked autolayout and used autosizing.

It usually looks like this

enter image description here

Main View looks like below in SIZE INSPECTOR.

enter image description here

Currently I am making two xibs, iPhone4 iPhone5 and use UIViewAutoresizingFlexibleTopMargin & UIViewAutoresizingFlexibleLeftMargin along with -64 in iOS6/7 Delta.

For Inner/Child Views, I usually Keep UIViewAutoresizingFlexibleTopMargin and UIViewAutoresizingFlexibleLeftMargin and -64 in iOS6/7 Delta to work fine in iOS6.

enter image description here

It give almost perfect result, but sometime I need to start View from start of xib (0,0) but sometime from (64,0) to show Views look like starting from upper most border of View while running.

It makes me confused a lot to understand what is standard way to design XIB, so that we can use single XIB for iPhone4 and iPhone5 and on both platforms.

Thanks

Upvotes: 5

Views: 2352

Answers (4)

Raj
Raj

Reputation: 413

As described in apple developer official website, the below link exposes the guidelines of using Visual Format Language for auto layout which one is the best practices adopted among the iOS developers nowadays.

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html

Upvotes: 0

gnasher729
gnasher729

Reputation: 52530

Best practice for using auto layout is to practice a lot until you get fluent using it :-(

You can then use several strategies.

The one that isn't going to work and that will make you pull your hair out is trying to combine interface builder and constraints in code. Don't do that.

The second that works after you figured out things is to do everything, design and layout using layout constraints, in interface builder. Note that layout constraints can be IBOutlets and that you can manipulate the constants of layout constraints in code quite easily.

The third that also works is to create your views in code, including the layout constraint. Use or write a library that makes creating constraints easier.

Upvotes: 0

Suraj Sonawane
Suraj Sonawane

Reputation: 2484

Writing Auto Layout code from scratch isn't easy.There is similar option available for AutoLayout stuff. We can set all constraints programatically with the help of PureLayout. https://github.com/PureLayout/PureLayout

Installation:

  1. Add the pod PureLayout to your Podfile.

    pod 'PureLayout'
    
  2. Run pod install from Terminal, then open your app's .xcworkspace file to launch Xcode.

  3. Import the PureLayout.h umbrella header.

    1. With use_frameworks! in your Podfile

      Swift: import PureLayout

      Objective-C: #import <PureLayout/PureLayout.h> (or with Modules enabled: @import PureLayout;)

    2. Without use_frameworks! in your Podfile

      Swift: Add #import "PureLayout.h" to your bridging header.

      Objective-C: #import "PureLayout.h"

You are done!. Now make some beautiful design with PureLayout

Upvotes: 0

yageek
yageek

Reputation: 4455

Personally, I find that using Interface Builder with AutoLayout is not very user friendly and that it quickly became messy.

I prefer do all AutoLayout stuff programmatically. To avoid lot of boilerplate, I use the Masonry library which is quite amazing.

Upvotes: 4

Related Questions