dimanche 28 décembre 2014

Setting iOS constraints inside a subview

I'm new to constraints so I'm a little lost setting things up correctly. What I'm trying to do is have a mainview setup with constraints and inside of that view add a viewcontroller where I can then use that viewcontroller's view properties to constrain objects inside of that viewcontroller. I have setup a simple program to demonstrate what I'm trying to do.


viewcontroller.m



#import "ViewController.h"
#import "SearchBarViewController.h"

@interface ViewController (){
UIView *mainView;
UIView *topView;
NSDictionary *viewsDictionary;
NSDictionary *metrics;
SearchBarViewController *sbvcSearchBarViewController;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//mainView---------------------------------------------------------------
mainView = [[UIView alloc] init];
[mainView setBackgroundColor:[UIColor grayColor]];
mainView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mainView];
//-------------------------------
viewsDictionary = @{@"mainView": mainView};
metrics = @{@"vSpacing":@20, @"hSpacing":@5};

//topView----------------------------------------------------------------
topView = [[UIView alloc] init];
[topView setBackgroundColor:[UIColor darkGrayColor]];
topView.translatesAutoresizingMaskIntoConstraints = NO;
[mainView addSubview:topView];

[self createMainConstraints];

[self createSearchBar];
}

-(void)createMainConstraints{
//Define the view Position and automatically the Size (for the mainView)
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[mainView]-vSpacing-|"
options:0
metrics:metrics
views:viewsDictionary];

NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[mainView]-hSpacing-|"
options:0
metrics:metrics
views:viewsDictionary];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS_H];

//TopBarContainer Constraints ------------------------------------------------------
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:topView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:mainView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:topView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:mainView
attribute:NSLayoutAttributeHeight
multiplier:.20
constant:0.0]];
//xLoc / yLoc
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:topView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:mainView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:topView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:mainView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
}

-(void)createSearchBar{
//SearchBarViewController-----------------------------------------------------------
sbvcSearchBarViewController = [[SearchBarViewController alloc] init];
sbvcSearchBarViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
[topView addSubview:sbvcSearchBarViewController.view];

//sbvcSearchBarViewController Constraints --------------------------------------
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:sbvcSearchBarViewController.view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:topView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:sbvcSearchBarViewController.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:topView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0.0]];
//xLoc / yLoc
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:sbvcSearchBarViewController.view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:topView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:sbvcSearchBarViewController.view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:topView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


SearchBarViewController.m



#import "SearchBarViewController.h"

@interface SearchBarViewController (){
UIView *searchBarSubView;
}

@end

@implementation SearchBarViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor purpleColor]];
//
searchBarSubView = [[UIView alloc] init];
[searchBarSubView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:searchBarSubView];
[self addConstraints];
}

- (void)addConstraints {
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:searchBarSubView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:searchBarSubView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0.0]];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

@end


SearchBarViewController.m's view is constrained inside of viewcontroller and this works without errors. But when I try and use the constrained view's properties inside of SearchBarViewController.m to constrain the searchBarSubView I get this nasty compile error:


"Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", "", "", "", "" )


Will attempt to recover by breaking constraint "


I have seen this same error many times in the past few days working with constraints and I made it this far but now I'm at a loss. Please help!


Thanks!




Aucun commentaire:

Enregistrer un commentaire