Moving UIButton on Y axis while using Autolayout - ios

I'm working on a project where I'm using Auto Layout / Universal Story board for the app.
I have a UIButton that I'd like to move to a new position on a certain event. But however without affecting the auto layout. So it will look the same on each device.
I've managed to move the button through this code:
int deltaY = 100;
[ans1 removeConstraints:ans1.constraints];
ans1.frame = CGRectMake(ans1.frame.origin.x, ans1.frame.origin.y + deltaY, ans1.frame.size.width, ans1.frame.size.height);
However the button moves back to it's orginal position within a second or so.

Why do you remove the constrains in your Button? instead of removing constrains, you can create an IBOutlet of the Button Y Constrain & then change that Y Constrain value
Select the LayoutConstraint you want to change, then control drag it to your .h file
#property (nonatomic, weak) IBOutlet NSLayoutConstraint *yAxisConstrain;
Change your Constrain value like this,
yAxisConstrain.constant = 0; //What ever the value you want.
[self.view layoutIfNeeded];

Related

Resize UILabel based on content inside UITableViewCell iOS

I am using a UITableViewCell which contains few ImageView and Labels. I have given the image of how the cell looks for your reference. I need the Content label to expand and contract based on the text within it, without disturbing any other views inside the cell. I am new to AutoLayouts and I am facing issues with it. Please help.
You need to add a NSLayoutConstraint in the storyboard and then hook it up to a property in your code. Here is an example of one I did with width, but you can do the same with height.
The referencing outlet in the .h file is:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintAuctionHouseNameWidth;
Then in the .m Controller file I figure out the size of the new label and set the constraint to that as shown below.
You then need to figure out the size of the text in your label, which is going to determine the size of the label to show it in. Here is how I do it: (auctionHouseObject.name is the text that goes into the label)
//calculate width of label
CGRect r = [auctionHouseObject.name boundingRectWithSize:CGSizeMake(350, 0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont fontWithName:#"Montserrat-Bold" size:15]}
context:nil];
Then I set the width programmatically. (auctionHouseNameLabelMaxWidth is different depending on the screen width of the phone)
if(r.size.width < auctionHouseNameLabelMaxWidth){
cell.constraintAuctionHouseNameWidth.constant = r.size.width + 2;
}
else{
cell.constraintAuctionHouseNameWidth.constant = auctionHouseNameLabelMaxWidth;
}
Set the other constraints in the storyboard and Autolayout should take care of the rest.

changing UITableView position programatically with AutoLayout support

I want to change position of UITableview position programmatically. I have added following code in viewDidLoad,ViewWillAppear callbacks, but position is not changing.
Table.frame = CGRectMake(0,100,320,200);
When I disable Auto Layout option in Storyboard, Table View's position is changing. But, I need to keep Auto Layout option to maintain support for different device type.
Use autolayout, and set constraints to your tableView's position & size.
Then, create IBOutlet instances of the type NSLayoutConstraint, and attach them to your constraints in the xib.
Then, in your code, set the values of the constraints to reset the table view position.
Sample code:
in your .h file:
#property IBOutlet NSLayoutConstraint *tableOriginX;
#property IBOutlet NSLayoutConstraint *tableOriginY;
#property IBOutlet NSLayoutConstraint *tableWidth;
#property IBOutlet NSLayoutConstraint *tableHeight;
Then, in your .m file where you want to alter your table's frame:
tableOriginX.constant = 0;
tableOriginY.constant = 100;
tableWidth.constant = 320;
tableHeight.constant = 200;

How do you change the top-padding for a view in iOS?

I want to change the top padding of a view to 80 when a button is toggled. How would this be done? Basically I want there to a space of 80 between the top of the view and the text.
If you are using autolayout, then the easiest thing to do is to create a constraint for the distance between the text and the top of the view. You can connect it to a NSLayoutConstraint in code, and set the constraint.constant to the new value when the button is pressed.
#property (nonatomic, weak) IBOutlet NSLayoutConstraint *paddingConstraint;
// Somewhere after the button press
self.paddingConstraint.constant = 80;
Oh, and you will probably need to update the layout by calling this -
[self layoutIfNeeded];

UIView animated changes with auto layout constraints

Say that I have a project which looks like the following:
There are two UIViews - one called yellowBox and the other called redBox. The auto layout constraints dictate that the yellowBox is 60 points from the top of the superview with 350 points leading and trailing spaces (i.e. to left and right of the view). The redBox has the same leading and trailing space constraints. There is a vertical space constraint between the two boxes of 0 to indicate that the bottom of the yellowBox should always be directly on top of the redBox.
When the user taps the Expand Yellow Box button, I would like the height of the yellowBox to increase and, as a result, the redBox moves down to continue satisfying the vertical space constraint (that the yellowBox is always on top of the redBox). Here is the animation code:
- (IBAction)expandYellowBox:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
CGRect newFrame = self.yellowBox.frame;
newFrame.size.height += 50;
self.yellowBox.frame = newFrame;
}];
}
However, I have been unable to get this working properly. As you can see by the red icon, there is a problem with the constraints at the moment:
which is fair enough, as there's no way for auto layout to know the height of the boxes. However, I don't know how to resolve this issue in such a way that it will allow for the boxes to be resized and moved. For example, if I specify a height constraint on the yellowBox then that will prevent it from being resized. If I set the height constraint to be greater than or equal (to allow the yellowBox height to increase) then I get a different constraint error:
All constraints have been established using Xcode in the storyboard - none have been written in code.
Any help greatly appreciated.
EDIT: As it turns out, the yellowBox is growing when the button is clicked - it's just I couldn't tell because it appears behind the redBox. I only noticed after clicking it around four times and it started appearing out the bottom of the redBox. However, the same question still remains - how to get the redBox to always stick to the bottom of the yellowBox.
Try it as mentioned by shwet-solanki, but add the following line after changing the constraint:
[self.view layoutIfNeeded];
the IBAction would look like:
- (IBAction)expandYellowBox:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
self.yellowBoxHeightConstraint.constant += 50;
[self.view layoutIfNeeded];
}];
}
Where yellowBoxHeightConstraint is the IBOutlet for height constraint of yellowBox.
Hope this helps.
Add Height constraint to both the views
Create an IBOutlet for height constraint of yellowbox
Now instead of changing the height of yellowbox in the button pressed event, rather change the value of the height constraint. i.e suppose your IBOutlet constraint name is yellowBoxHeightConstraint, then yellowBoxHeightConstraint.constant += 50;
Hope this works for you.
//
// WPViewController.h
// AutoLayoutTEst
//
// Created by VASANTH K on 09/01/14.
//
//
#import <UIKit/UIKit.h>
#interface WPViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *button1;
- (IBAction)buttClicked:(id)sender;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstrain;
#property (strong, nonatomic) IBOutlet UIButton *button2;
#end
click Event
- (IBAction)buttClicked:(id)sender {
self.heightConstrain.constant=100;
}
here u need to set the heightConstrain for the Yellow button then create reference outlet for that button then update the heightConstrain to update the size of that button it will automatically move your Red button down.
https://github.com/vasanth3008/AutoLayoutHeighDemo
refer my sample demo project

Animating/Moving views under usage of Autolayout

I want to move a view from one postion to another, I can implement it using
self.view.center = CGPointMake(100, 200);
however, if the project is using Autolayout, then the view will be back to original position after running:
[self.view.superview setNeedsLayout];
then how to actually move a view to new position?
With AutoLayout enabled, we should FORGET FRAMES and only CONSIDER CONSTRAINTS.Yes, for animating also you can no longer change the frame or center, view's will revert back to their original position when layout is called.
Instead you should consider changing the constant value of the constraint to get the same effect.
Consider a User Interface like the image given below.I have a image view with a 20 points leading space from it's superview that means it has a horizontal space constraint with it's superview.Also I have three more constraints attached to that image view, top, width and height.
I will show you how we can animate the image from left to right as shown in the image.
Create IBOutlet's for the constraint's we need to animate.Here we are taking only horizontal space constraint, that is enough to move this view from the left to right.
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalSpaceConstraint;
Inside Go Away action, we need to update the constant value of this constraint.
- (IBAction)moveFrontAction:(id)sender {
self.horizontalSpaceConstraint.constant = 220;
[UIView animateWithDuration:0.5 animations:^{
[self.imageView layoutIfNeeded];
}];
}
Now the view should be moved to the right end.I'm just doing that inside a animation block so we will be able to see a nice animation from left to right and vice versa.In Production, we should not hard code the values like this.Just doing it here to make the concept clear.
Inside the Come Back action, we are again resetting the constant back to it's original value, so you can see the orange image animating back to the original location.
- (IBAction)moveBackAction:(id)sender {
self.horizontalSpaceConstraint.constant = 20;
[UIView animateWithDuration:0.5 animations:^{
[self.imageView layoutIfNeeded];
}];
}
You must change the constraints if you are using autoLayout. The way that is suggested is to make an outlet in your view controller of the constraint, then you change the constant of the constraint. If you have the time i would definitely recommend going here and watching "Auto Layout by Example" or "Best Practices for Mastering Auto Layout". They helped me out a lot. I guess the point to take away is that with autoLayout, you no longer think in terms of frames. So setting the center just doesnt work with auto layout. It's all about how views are related to each other.

Resources