title: Where to add topLayoutGuide constraint code
body: <h2>Solution</h2>
  
  <p>Figured out a solution, put the following code in the <code>viewDidLoad</code> method of my subclassed navigation controller .m file:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
  
      if ([[[UIDevice currentDevice] systemVersion] floatValue] &gt;= 7.0f) {
          [[self view] setTranslatesAutoresizingMaskIntoConstraints:NO];
  
          id topGuide = [self topLayoutGuide];
          UIView * selfView = [self view];
          NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (selfView, topGuide);
          [[[self view] window] addConstraints:
           [NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[selfView]"
                                               options:0
                                               metrics:nil
                                                 views:viewsDictionary]
          ];
          [[[self view] window] layoutSubviews]; // You must call this method here or the system raises an exception
      }
  }
  </code></pre>
  
  <h2>Original Post</h2>
  
  <p>Apple's <a href="https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/topLayoutGuide" rel="nofollow">doc</a> didn't say it clear that where (which class, which method) should I put this chunk of code (don't know what does <code>self</code> refers to in the code):</p>
  
  <pre><code>[button setTranslatesAutoresizingMaskIntoConstraints: NO];
  id topGuide = myViewController.topLayoutGuide;
  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
  [myViewController.view addConstraints:
      [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
                                                   options: 0
                                                   metrics: nil
                                                     views: viewsDictionary]
  self.view layoutSubviews; // You must call this method here or the system raises an exception
  ];
  </code></pre>
  
  <p>And I feel that the above chunk of code has some typo, so here's what I think it should be:</p>
  
  <pre><code>[button setTranslatesAutoresizingMaskIntoConstraints: NO];
  id topGuide = myViewController.topLayoutGuide;
  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
  [myViewController.view addConstraints:
      [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
                                                   options: 0
                                                   metrics: nil
                                                     views: viewsDictionary]
  ];
  self.view.layoutSubviews; // You must call this method here or the system raises an exception
  </code></pre>