I have two buttons on my UIView. I want the horizontal distance between these two buttons should, be the 20% width of the superview?
Any help about this?
You can accomplish this using an empty dummy view in between the two buttons, whose width is 20 % of the width of the superview.
In code, it would look something like this:
// The important thing here is that the buttons are flush against the spacer
[superview addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:#"[button1]-0-[spacer]-0-[button2]"
options:0
metrics:nil
views:#{#"button1" : button1, #"button2" : button2, #"spacer" : spacer}]];
// Here, we set the width of the spacer to 20% of the super view
[superview addConstraint:
[NSLayoutConstraint
constraintWithItem:spacer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeWidth
multiplier:0.2
constant:0]];
Related
I have a group of views in my view controller, each set with a constraint of 15 from the previous. When I press a button elsewhere on the VC, I have one of the views, near the top, double in height. How do I get the rest of the views to snap to their new constraints?
NSLayoutConstraint *sample = [NSLayoutConstraint constraintWithItem:yourTargetView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:yourTargetsParentView
attribute:NSLayoutAttributeLeft
multiplier:0.00
constant:100];
[yourTargetsParentView addConstraint:sample];
How to add constraints programmatically for views which should be placed next to each other on scroll view?
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1 constant:10];
[self.scrollView addConstraint:constraint];
constraints are basically a equation iOS will try to satisfy at runtime.
General form is:
item1.attribute = multiplier * (item2.attribute) + constant
In the code above:
view2.left = 1 * (view1.right) + 10
So view2's left will be at 10pt space from view1's right.
I have a view controller set up in a storyboard. The view controller's view contains a subview(UITableView) with pinned edges to the 4 sides of its parent, essentially making the view fill its parent.
I am adding the view controller's main view as a subview of another view controller's view like this:
UIView *overlayView = firstViewController.view;
[overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *sourceView = secondViewController.view;
[sourceView addSubview:overlayView];
NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:overlayView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:sourceView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
[sourceView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:sourceView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:overlayView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:100];
[sourceView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:overlayView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:sourceView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[sourceView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:overlayView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:sourceView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[sourceView addConstraint:constraint];
[sourceView layoutIfNeeded];
I want to have a gap from the right edge of the view to its parent equal to 100 pixels.
Now weirdly enough this works as expected on iOS 8, but fails to do so on iOS 7 and the view is displayed full-screen ignoring the 100 constant set on the trailing constraint. Also, if the view controller's view that is being added has no child views - then it is working as expected. Is setTranslatesAutoresizingMaskIntoConstraints: being applied to all the subviews of a view in iOS 7 ? What might be the reason for this ?
EDIT:
The issue lies somewhere in the way subviews are being managed by the OS.
When the Container View is set as a outlet to the view property of the view controller, things don't work. If however, I set the Table View as an outlet to the view property, the it works. Something weird happens if there is a child view with pinned edges to its superview and then I am adding other constraints to the superview. I dont understand why it works fine on iOS 8 though...
EDIT 2
The problem seems to happen only with the trailing constraint. If I want to modify the constants of any of the other constraints there are no issues ?!
Can you try interchanging sourceView with overlayView in your constraint?
`constraint = [NSLayoutConstraint constraintWithItem:sourceView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:overlayView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:100];`
This essentially means that sourceView.trailing - 100 = overlayView.trailing; which is not what you would want.
Edit 1:
Is setTranslatesAutoresizingMaskIntoConstraints: being applied to all the subviews of a view in iOS 7?
NO. translatesAutoresizingMaskIntoConstraints is set only for the views you explicitly call setTranslatesAutoresizingMaskIntoConstraints for.
I am trying to centre a subview (200 wide) within a smaller superview that is 100 wide. Using the constrains below, it ends up at x=-100 instead of x=-50.
[NSLayoutConstraint constraintsWithVisualFormat:#"H:[statusLabel(200)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:viewDict];
The alignment constraints, like NSLayoutFormatAlignAllCenterX are for aligning multiple sibling objects with respect to each other, not for aligning a single object with respect to its superview. To do that, you need to useā¦
NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:self.outerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.innerView
attribute:NSLayoutAttributeCenterX
multiplier:1.f constant:0.f];
I will be short and very clear. I want to do what's on the figure below using constraints.
Any suggestions or solutions?
Description:
The coloured places are UIViews, containing for ex. 4 labels. So what constraint should I use to manipulate with the second UIView so in Portrait mode to be under the first one and in Landscape to be next to it?
The code below assumes you already have a reference to the orange view and the yellow view in your code. When in portrait mode you want them to be in sequence so you can have a layout as such
NSLayoutConstraint *portraitConstraint = [NSLayoutConstraint
constraintWithItem:orangeView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:yellowView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:2.0f];
[self.view addConstraint:portraitConstraint];
When in landscape mode you can have a layout constraint as such
NSLayoutConstraint *landscapeConstraint = [NSLayoutConstraint
constraintWithItem:orangeView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:yellowView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:2.0f];
[self.view addConstraint:landscapeConstraint];
Now these are not the full list of constraints you would need, if you build in the code and only the code you would have to have the orange view stick to the top, leading and trailing of the view and then in the code have the yellow view stick to the leading, trailing and bottom in the view for portrait.
In landscape you would have the orange view stick to the top, bottom and leading while the yellow view would stick to the top, bottom and trailing.
The constraints that are above would allow so you do not need a height to be set, but you might want to also say something like the orange view bottom is centerY - 1.0 in portrait and centerX - 1.0f in landscape thus avoiding the need for widths and heights and hence not worrying about the size of the screen. Center X and Y are below
NSLayoutConstraint *centerX = [NSLayoutConstraint
constraintWithItem:orangeView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:-1.0f];
[self.view addConstraint:centerX];
NSLayoutConstraint *centerY = [NSLayoutConstraint
constraintWithItem:orangeView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:-1.0f];
[self.view addConstraint:centerY];
The above constraints should help you on your way to resolving the issue.