0
votes

I am adding a view programatically and changing constant of height constraint but it does not set the width as the screen width in viewDidLoad method. But width perfectly set when put those line in after 0.2 seconds delay queue.

Below is my Code

Added In ViewDidLoad

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ cAreaView = [[[NSBundle mainBundle] loadNibNamed:@"aView" owner:nil options:nil] objectAtIndex:0]; [_vwComplaintArea addSubview:cAreaView]; [self setConstraintLayout:cAreaView toVw:_vwComplaintArea]; [_firstViewHeightConstraint setConstant:_firstViewHeightConstraint.constant + cAreaView.bounds.size.height]; [_heightForComplaintARe setConstant:cAreaView.bounds.size.height]; });

-(void)setConstraintLayout:(UIView *)vw toVw:(UIView *)toVw { [vw autoSetDimensionsToSize:CGSizeMake(toVw.bounds.size.width, vw.bounds.size.height)]; [vw autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:toVw]; [vw autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:toVw]; }

Can anyone suggest the best way to set constraints programatically in viewDidLoad.

Thanks.

3
Try to set constraint in viewWillAppear.Tony
But it showing some animation of changing height of View. I dont want any kind of animation there.user1624741
Could you provide yours code that not working and working?Cy-4AH
add your code in question what you have tried!Ketan Parmar
add it in question!!! not in comment!!!Ketan Parmar

3 Answers

0
votes

I would suggest you to override viewWillLayoutSubviews and viewDidLayoutSubviews. In viewDidLoad you should only add elements to you view from xib, any manipulation for the frame should occur in view will and did layout subviews. It has to do something with view cycle.

Per documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/viewDidLayoutSubviews

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method.

On this event, you will have the correct situation about screen and distances so you can update constraints and frames accordingly.

0
votes

You can change constraint constant at any time you wish. But as I understood, you are trying to set constant equal to view width value, taken from view's frame or bounds property. You need to know, that in viewDidLoad this properties don't have actual values since you use autolayout; they will get actual values in viewDidLayoutSubviews method (note that it can be called multiple times). When you add delay for 0.2 sec, autolayout processes already finished, thats why you get right result. So, you can assign constraint constant in viewDidLayoutSubviews, OR better just add new constraint for equal width's:

let someProportion: CGFloat = 2
view.addConstraint(NSLayoutConstraint(item: yourView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: someProportion, constant: 0))
-1
votes

Don't set your constraints in viewDidLoad. Instead set them in viewDidAppear or viewDidLayoutSubviews. So that your app will have all the components loaded for applying constraints.