i'll just address the dyanmicView being part of the parentView issue, then let you go from there
first: if you are creating the view dynamically, then you're good to go, but if you've created it from storyboard, you'd have to detach it from it's parent then reattach it.. that's how you get rid of it's previous NSConstraints (that usually the storyboard introduces) that may conflict with your new ones.
you also gotta set it's setTranslatesAutoresizingMaskIntoConstraints
to NO b/c that also can interfere with your nsconstraints.
I usually do those last two steps like so, using mapObjectsUsingBlock to make the whole tedious process of creating constraints a bit more pleasant and natural:
[@[view_1, view_2, /../, view_n] mapObjectsApplyingBlock:^(UIView *view) {
[view removeFromSuperview];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[view setHidden:NO];
[superView addSubview:view];
}];
then before applying nsconstraints, you gotta make sure that the view you want the constraints to apply to is already attached to it's parent:
[parentView addSubview:dynamicView];
then you want to create a bindings dictionary:
NSDictionary *buttonBindingsDictionary = @{ @"parentView" : parentView,
@"dynamicView" : dynamicView};
then you want to add the constraints using the visual format language.. I also use mapObjectsUsingBlock
here (i'll explain each constraint in english):
NSArray *buttonConstraints = [@[@"V:|-[dynamicView(>=200)]-|",
@"|-[dynamicView(>=260)]-|",
] mapObjectsUsingBlock:^id(NSString *formatString, NSUInteger idx){
return [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:buttonBindingsDictionary];
}];
V:|-[dynamicView(>=200)]-|
means that vertically speaking.. the upper and lower distance between dynamicView
and it's parent should be equal.. also dynamicView's
height should be no less than 200
|-[dynamicView(>=260)]-|
means that horizontally speaking.. the left and right distance between dyanmicview
and it's parent should be equal.. also dyanmicView's
width should be no less than 260
note: you can do the math yourself and set exactly how much the left/right/bottom/top distance between dyanicView
and it's parent.. this is just simpler.. but sometimes nsconstraints messes up and I gotta do it myself.
in that case it would look something like this where x
is the distance you came up with:
V:|-x-[dynamicView(>=200)]-x-|
|-x-[dynamicView(>=260)]-x-|
then you gotta add the constraints to the parent view:
[parentView addConstraints:[buttonConstraints flattenArray]];
notice here i used flatten array, again that's a method from my library b/c i want to feed it a one level array, not an array of arrays.
and you're good to go!
note: i know that this may not work perfectly.. but it gives you the idea of what to do + some helper files. It takes some practice, and you should look at some of the tutorials for sure.. i suggest you start with nsconstraints using storyboard (you can select a view.. then go editor>pin>.. then chose something.. get some immediate visual feedback.. also you can simulate what your views will look like in 3.5" displays vs 4.0" displays immediately on the storyboard by selecting the view controller, then going to attributes inspector and selecting different sizes under simulated metrics)
take your time bud, but one thing for sure: once you go nsconstraints you will never look back! it's totally worth it!
p.s. you can also animate views using nsconstraints as well.. just in case you were wondering.