0
votes

I am trying to create entire iOS application without using Interface Builder at all. I am facing some problems with auto layout.

My AppDelegate.m file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[MainViewController alloc] init];
    [self.window makeKeyAndVisible];
    return YES;
}

And my MainViewController.m file

@interface MainViewController ()
@property (nonatomic, strong) UILabel *label;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *superview = self.view;

    // Configuring view

    NSLog(@"Constraints: %@", self.view.constraints);
    self.view.backgroundColor = [UIColor whiteColor];

    // Configuring label

    self.label = [[UILabel alloc] init];

    self.label.text = @"text";
    [self.label sizeToFit];
    self.label.frame = CGRectMake(8, 20, self.label.frame.size.width, self.label.frame.size.height);
    [self.view addSubview:self.label];
    // Do any additional setup after loading the view.

    [self.label addConstraint:[NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0]];
}

This code gives me this exception message:

2015-08-11 22:12:53.940 AutoLayoutWithoutInterfaceBuilder[5088:130112] The view hierarchy is not prepared for the constraint: (Names: '|':UIView:0x7d53f190 )> When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug. 2015-08-11 22:12:53.941 AutoLayoutWithoutInterfaceBuilder[5088:130112] View hierarchy unprepared for constraint. Constraint: (Names: '|':UIView:0x7d53f190 )> Container hierarchy: > View not found in container hierarchy: > That view's superview: NO SUPERVIEW 2015-08-11 22:12:53.944 AutoLayoutWithoutInterfaceBuilder[5088:130112] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: view:>'

When I created a single page application with IB, in viewDidLoad I have printed constraints of self.view.

NSLog(@"Main view constraints: %@", self.view.constraints);

It can be noticed that the view contains 4 constraints, while in Interface Builder they are not listed.

How can I setup Auto Layout in my application purely with code? I don't want to use Interface Builder at all, but I am pretty confused. Are there good guides about Auto Layout entirely by code?

Which constraints I have to add in code after initializing ViewController? What setup do I have to perform to make autolayout work properly?

1

1 Answers

0
votes

You need to add the constraint to self.view, not self.label. Both items in the constraint have to be descendants of the view you are adding your constraint to.