iOS: Set View Size in Code with Autolayout enabled - ios

I have two view controllers inside a tab bar navigation. Inside the second scene I have an additional view (just a simple UIView) and a button to set it's color and bounds.
CGRect viewRect = CGRectMake(20, 20, 70, 70);
self.animationView.bounds = viewRect;
self.animationView.backgroundColor =
[UIColor yellowColor];
This code works fine. But if I navigate to the first view controller and then back to the second view controller my view is still yellow but it is back at the size and position I set in interface builder.
How can I prevent this?
This behavior ends if I disable autolayout but I don't really want to do that.

Create outlets for the animationView constraints, and change their constant value.
In the .h file of the viewcontroller:
Connect the outlets to the correct constraint in the IB:
//AnimationView Height Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cHeight;
//AnimationView Width Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cWidth;
//AnimationView Leading Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cLeading;
//AnimationView Top Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cTop;
In the .m file of the ViewController set the constant value of the constraints, instead of the frame:
- (IBAction)btnTouched:(id)sender {
[_cHeight setConstant:70];
[_cWidth setConstant:70];
[_cTop setConstant:20];
[_cLeading setConstant:20];
self.animationView.backgroundColor = [UIColor yellowColor];
}
It'll work fine.

Related

UIProgressView have different position at runtime

I place a UIProgressView on an XIB file. At first it goes well. After when I set its height via auto layout to be 20, problem occurred. The Progress View always got its position at the most top-left of the screen. even I use
[self.progress setFrame:CGRectMake(93, 100, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
still don't work for me at all.
Thank you very much for any help.
use size constraints as outlets. Then you can easy change values of those constraints. It's not really recommended to modify frame when using auto-layout.
declare a property:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
and in the implementation:
self.heightConstraint = self.scrollView.frame.size.height;
self.widthConstraint= self.scrollView.frame.size.width;
Alternatively you can try to set frame in viewDidLayoutSubviews but it's not recommended when using auto-layout.

Clips subviews does not work with auto layouts constraint

I have UIView with auto layout constraint. My view contains subview. I have height constraint that I attached to my view controller as a property.
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeightLayoutConstraint;
In view did load I set:
[[self headerViewHeightLayoutConstraint] setConstant:0.0f];
But I still see subviews. So I check Clip subviews in storyboard for that view but I still see subview.

How to resize my view programmatically using autolayout

I am designed my screen in xib using autolayout(xcode 6). I have to redesign the screen programmatically using autolayout.
I have 4 views in my screen horizontally. I have to hide the third view and move the fourth view to third view's position using autolayout.
How do I update the constraint programmatically.
You may use NSLayoutConstraint and assign proper IBOutlet to any of your constraints and then where you wants to change the view size use constant property of NSLayoutConstraint to change the value.
e.g
in your viewController.h
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *tempConstraint;
some where in your viewController.m
tempConstraint.constant = 50; // or what ever you want

How to set autolayouts and content size in xib for multiline label in scroll view

I am using autolayout. In xib i have a scrollview as a parent view. Added one view as a subview. For that subview i have added four child elements. Three are views(say view1,view2,view3) and one is label which is having multiline text.
I am using the below code to change the height of the view
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *view1Height;
self.view1Height.constant = 200;
In this case there is no issues with content size the view2 and view3 frames are adjusted automatically.
But while coming to multiline label I am using the below code for setting label text
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *textLabelHeight;
self.textLabelHeight.constant = expectedLabelSize.height;
But here I am facing issue with scroll content size. Unable to view the entire text. Similarly I want to place one more view below the label how to set constraints for it.

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;

Resources