Updating constraints programmatically - ios

Situation is like I have a view with four buttons
There comes a condition when button with text Search Community should be hidden and width of Options button should get increased
Using Constraints for the first time in my project, I am not getting how to achieve this. Set of constraints added on both these buttons would be clear from the following two images.

The problem I see with your setup is that you have this constraint between the 2 buttons of equal width. This is true in the first case but when you want to hide one of them, it is not anymore.
So, you will need to rethink your constraints a bit. Maybe instead of the equal width constraint, you could use a static width constraint one the first button(the left one, that you want to hide). Then the second one, will just have a horizontal space constraint to the first one, and a trailing space to the superView.
Then you make an outlet in the VC for the width constraint of the first button and when you want to hide it you do something like this:
self.searchButtonWidthConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
Let me know if you have questions. Good luck and hope it works out!
Update
For the landscape use case, you could listen to the orientation change notification, to update the width constraint of the button
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.searchButtonWidthConstraint.constant = LANDSCAPE_WIDTH
UIView.animateWithDuration(duration, animations: { () -> Void in
self.view.layoutIfNeeded() // if you want to animate the constraint change
})
}

Add constraint to 1st Button
Leading Space To NewView(gray colored)
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Width (from Bottom - Right portion )
Add Constraint to 2nd Button
Trailing Space to NEwView
Bottom space to NewView
Top Space to Topbutton (blue colored in your 1st and 2nd image)
Horizontal space FirstButton ( Search Community )
Width

Related

Re-position uilabel

I wanted the "number" & "status" to display on the left if there is not QRCode display (constraints has been set for image & labels.)
Result without QRCode:
.
The result with QRCode will be:
IT IS very easy just make width Constraint of QRcode image, if QR code is not available than make widht constant = 0,
#IBOutlet weak var width: NSLayoutConstraint!
if (your condition)
{
width.constant = 0
}
else
{
width.constant = 30
}
You can do that with a second pair of constraints attached to left side of a view.
Add outlets to that constraints and set constant value to desired offset(from code), if there is no QRCode View.
Another way is to add MoreThanOrEquel constraint to left side (e.g >= 10)
Than another constraint with constant value 10, but with lower priority.
When you remove QRCode view, it will destroy it constraints and constraints to left side will affects and move yours labels to left.
You could make constraint for labels that way:
I.e. make constraint for image leading to left side + image width, both labels also have leading to left side. Then if there is no QR Code adjust labels leading constraints to value you need.
Or you could make both labels leading to image view and set the image view width constraint to 0.
This Problem is very simple , can achieve using UIStackView just making hide/Show.
For Ex: If QR-code is present show or else hide QR-Code.
Label (Number and status and image) will automatically get adjust. Following is the screen short for better understanding.
IBOutlet UIView *qr_view; // Image or View
Case 1 :QR Present
qr_view.hidden = NO;// Show
Case 2: QR Not Present
qr_view.hidden = YES;// hide

Swift: Views not adjusting to programmatic constraints

I have a view that I am making hidden at the bottom of the screen, and want the scrollView above it to adjust and fill the void space.
The view at the bottom of the screen is a GADBannerView and has a fixed height of 50 (bannerHeight). The scroll view above it has a constraint to the bottom of the container that equals 50 (scrollConstraint). See photo.
In viewDidLoad is am setting these constraints to the following:
bannerHeight.constant = 0
scrollConstraint.constant = 0
This is causing the bannerView did disappear but the scroll view is staying in it's original position and not filling the void space.
You can force the superview to take into account the change of the constraint because this does not happen automatically. Add your code to viewDidLayoutSubviews() instead or simply call view.layoutIfNeeded() after you set the constants to 0 in the viewDidLoad().
If this does not work, you can try this alternative approach:
Go to your Storyboard and click on the scroll view's bottom constraint (the blue line that gives the scroll view its bottom constraint of 50). In the Attributes Inspector you should be able to see details about your constraint, it should look something like this
In the field that asks for an Identifier, give it the name "ScrollViewBottom" or whatever name you like.
Now loop over all the constraints that make up your scroll view and use the identifier name to find the correct one and change it's constant as follows
for constraint in yourScrollView.superview!.constraints {
if constraint.identifier == "ScrollViewBottom" {
constraint.constant = 0
}
}
Finally, force the view to take into account of this change by calling the following straight after
view.layoutIfNeeded()

Animating constraint on a pair of views (or keeping content constraints within an animating view)

I'm coding in Swift 2.0 with Xcode.
I have a UITableView that has a constraint to the top of the view. It is also constrained to the other 3 edges of the view but only the top constraint varies.
A label is then paired with the table with top/bottom/left/right constraints so it exactly relates to the size and position of the tableview.
I am animating the table constraint with the following code.
self.view.layoutIfNeeded()
self.myTableViewTop.constant = 300
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.view.layoutIfNeeded()
}) { (completed) -> Void in
// process completion stuff here
}
I expected that the label that is tied to all dimensions of the table would resize in accordance to the animating table size but it doesn't seem to.
What happens is the label reverts immediately to its final size and waits for the animated table to catch up.
What am I missing here? Something to do with priorities? Or a different way to tie the 2 elements together? Or do I need to animate both at same time?
Thanks.

Is it possible to change the bottom layout guide y position?

I would like to move the content of the whole screen up to make room for a view that I would add to the main window. Is it possible to adjust the bottom layout guide y coordinate, and make the whole view structure move up?
thanks.
ios8+
You can create an outlet to the bottom constraint of the view, and change its value from your code. UIView animations works fine in this case :
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.verticalBottomConstraint.constant = 44
)
Yep, the constant property of a NSLayoutConstraint is variable while the multiplier property is constant. Logic everywhere.

Adjust UIView width when other views increase or decrease in size

I have a UIView with a side menu that comes from the left and pushes the right view from full with to a smaller size (full width - (menu width)).
Per someones suggestion I accomplish that effect by changing the constant in the constraint for the menu width: from 0 to 200.
The (right side view) UIView that's gonna hold the view that I will load to it has the constraints seen in this image (menu is on the left in blue):
I add the new UIView to the detailsView (container mentioned above) with the following code:
var viewNames = NSDictionary.FromObjectsAndKeys (new NSObject[] {
view.View
}, new NSObject[] {
new NSString ("detailsView")
});
detailsViewContainer.AddSubview (view.View);
view.View.TranslatesAutoresizingMaskIntoConstraints = false;
detailsViewContainer.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|[detailsView]|", 0, new NSDictionary (), viewNames));
The problem is that the newly added view always has the full width (1024) when the menu is collapsed but also when the menu is expanded, pushing the view outside the app limits on the right.
Any help would be appreciated.
Thanks in advance!
Make the master view width constraint (add one if there is no) to be <= 1024 (not strict). I would do the following:
|H:|-0-menuView-masterView-0-|
and specify in designer the width constraint for masterView as Less or Equal 1024
and specify in designer the width constraint for menuView as equal to 200. Make an outlet for it and change dynamically in code with
animation from 200 to 0 and back when required.
where:
menuView is menu UIView
masterView is master UIView placeholder
when your menu (green view) is open width constraint will be equal 200:
as soon as you set constraint to 0 your view will resize the main placeholder (orange view) as well:
Please find storyboard sample by the following link:
https://dl.dropboxusercontent.com/u/19503836/Main.storyboard

Resources