Change height constraint programmatically - ios

I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;
To change it I do the following:
self.descriptionHeightConstraint.constant = self.description.contentSize.height;
This works if I do it in viewDidAppear but the problem with this is that I can see how the height changes after the view displayed which is not very user friendly so I tried doing it in viewWillAppear but didn't work there, the height doesn't change. Calling setNeedsUpdateConstraints after changing the constraint didn't work either.
Why is working in viewDidAppear and not in viewWillAppear? Any workaround?
Thanks in advance!

Try setting the constant in viewDidLayoutSubviews instead.
Note that using the text view's contentSize property to set the text view's height does not work in iOS 7. See https://stackoverflow.com/a/18837714/1239263

Related

Change the width of a UIView that already exists in Storyboard? Swift

I already have created a UIView in the storyboard and connected it as an outlet, with the name progressView
In Swift, I want to be able to change the width of this view in a certain function, however, so far I have been unable to.
I would really appreciate any help.
I've just write an example , if your project is autolayout , you need to define width constraint for progressView , then you need to make an outlet connection from your constraint and then change this property at method
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *ViewWidth;
in method:
self.ViewWidth.constant =number;

Where do I update my constraint before the view is on screen?

I need to update some NSLayoutConstraints before the view comes on screen depending on the device's screen size.
What I want to do is that this update becomes invisible to the user's eyes.
And I do not know where I can do that because :
- if I do this in :
- (void)viewWillLayoutSubviews
of my UIViewController it is called twice so i make twice the calculation on my constraint and I do not want to set a bool or a counter...
- if I do this in :
- (void)viewDidLoad
it does not work because I calculate my constrain's value based on the view of the view controller which is 0 height at this moment.
I see that there are a lot of methods for auto layout (setNeedsUpdate ...)
and if someone could tell me the best way to achieve an update of the constraints only once in the viewController lifeCycle that would be great.
Thanks for any help! (N.B: the auto-layout concept is new to me.)
Try overriding updateViewConstraints on your view controller. (Don't forget to call super ;) )
See Apple docs here
You can create the outlet of your height constraint from storyboard.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightForYourView;
& in viewDidLoad write the following code which will automatically change the size
//if you want to change the size as per device check device type in my case it iphone6
if (IS_IPHONE_6)
{
self.heightForYourView.constant=250;//height you want
}

UITableView Auto-Layout Flexible Height

I have following design.
Am using AutoLayout to make every thing flexible. In this design i have a UIView which is Gray in Color as showing in image and a UITableView below UIView. Some times i have to show this UIView and some times i have to hide this Gray UIView.
My problem is when ever i hide UIView, UITableView is not fixing its height. I don't want to hard code in .m file. Is it possible AutoLayout take care of this issue. I have these constraints as below image. Am i missing any constrain.
When i try to change UIView height, UITableView is not moving up and showing some orange constraints as show in image.
The contraints looks good. All you have to do to show/hide the gray UIView is change the height constraint constant.
To do this, create an IBOutlet in your controller for the constraint (you do this the same way you would for a UIView IBOutlet), and when you want to hide the gray view, set the constant property of the constraint to 0.
eg.
#interface MyViewController
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *greyViewHeightConstraint;
#end
and when you want to hide the view:
self.greyViewHeightConstraint.constant = 0;
To show the view again, you would have to save the "default" constant value after the storyboard is loaded (like in viewDidLoad for example), and set self.greyViewHeightConstraint.constant to this saved value.
Note also that these constraint changes can be animated.
The "orange constraint" you are seeing in Interface Builder is normal: it indicates that the view frame is not matching the constraints you set. You can then update the frame to respect the constraints, or update the constraints to match the frame.

Stop autolayout from resizing my view

Using Storyboards I add a UIView with height of 5px.
At run time the height of that view is increased (could be up to 25px).
When the orientation changes, the height of the view is reverted back to the original 5px.
How can I prevent this? I want the height to remain at whatever it was prior to the orientation change.
Sure I could detect orientation change and then change it back but that looks sloppy because it shrinks then increases right away.
Edit: This may or may not be an autolayout issue. Or might just be the default behavior of storyboards.
Add a constraint fixing the height, you may also need to remove a constraint. If you show the code or explain how you're setting the constants in the first place can give a more detailed answer.
Other answer is to remove the constraint that stretching the size of your view, may be that you have conflicting constraints.
One more answer could be you need to adjust the priority of an other constraint perhaps one for spacing between views. Again more detail = better answer.
Overriding -layoutSubviews is the incorrect approach. If you're using autolayout you should adjust a view's height by modifying its constraints programmatically.
Make a property for your view's height constraint:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
I put IBOutlet there since you said you're making the constraints in a storyboard. Just drag the height constraint to that property to connect it. (You could also create the constraint programmatically, in which case you wouldn't use IBOutlet.)
Then, whenever you want to change the height:
self.heightConstraint.constant = 25;
The solution to the problem is to sublcass UIView and override -(void)layoutSubviews
In -(void)layoutSubviews is where I set my desired height

UIScrollView only scroll with at least 1 object inserted from Interface Builder

I have a weird behaviour here : my UIScrollView only want to work when I had at least 1 object from Interface Builder.
Because I also add some subviews dynamically like :
[scrollView addSubview:player.view];
But it doesn't want to work except if I drop at least 1 item from IB into scrollView.
Note that to declare my scrollView in my header file I drag and dropped using IB and i created an IBOutlet :
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Then I synthesize it.
Anyone experienced the same issue ?
Thanks for help and ideas.
As far as i know, you need to write at least a little code for the UIScrollView to work, at least contentSize must be set.
Add another IBOutlet for your content (eg. scrollContent) in the scrollview and set the scrollview contentsize to the bounds of the scrollContent in viewDidAppear
Try debugging your code. Probably your contentSize is zero in viewDidLoad. Thats normal, the size of the view is only know at viewDidAppear.
For sample code, see http://bitbucket.org/robvanderveer/scrollviewdemo.

Resources