Where to add topLayoutGuide constraint code

Solution

Figured out a solution, put the following code in the viewDidLoad method of my subclassed navigation controller .m file:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 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
    }
}

Original Post

Apple’s doc didn’t say it clear that where (which class, which method) should I put this chunk of code (don’t know what does self refers to in the code):

  • Swift - Return data from NSURLSession when sending POST request
  • Charles proxy SSL connections won't show hostnames, only ips
  • How to use Optionals to stop a compiler error in Swift
  • topviewcontroller for uitabbar
  • iOS performSelectorOnMainThread with multiple arguments
  • manual language selection in an iOS-App (iPhone and iPad)
  • [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
    ];
    

    And I feel that the above chunk of code has some typo, so here’s what I think it should be:

    [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
    

    Solutions Collect From Internet About “Where to add topLayoutGuide constraint code”

    In that case, self can refer to a view controller. With this code, your are adding constraint to its view, so it can layout it subviews as you set them when calling layoutSubviews. If you add this code in the viewDidLoad method (and I recommand you to add it there) you can replace occurrences of myViewController by self