changing UITableView position programatically with AutoLayout support - ios

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;

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.

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.

Change frame programmatically with auto layout

I have a UITableView with Auto Layout and I need to reduce the height when the GADBannerView appears at the bottom of the screen.
Unfortunately with Auto Layout it is impossible to modify the frame. The solution is to remove Auto Layout and set the frame manually. This is very dangerous because all my apps works fine on 3.5' and 4.0' displays and removing Auto Layout adds a new testing phase and more effort.
Is there a way to change the frame even if Auto Layout is enabled?
Let your UITableView constraints to bottom layout is set to 0, make an IBOutlet. Now let your GADBannerView height is 40 so change your outlet.constant = 40; For more about how to make IBOutlet and change its value have a look into this or this hope this will help.
Edit: For those who seeking for example, follow these simple steps (Because this is accepted answer, I think it is worth to have an example. Credit to #manujmv for this example)
Create a height constraint for your view in your interface.
Then add an IBOutlet object in your class for this constraint.
For example:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint
*heightConstraint;
Connect this object in your connection panel.
Then change the value of this constraint whenever you needed
self.heightConstraint.constant = 40;
Rather than trying to change the frame of the view, add a height constraint using auto layout and reduce the value of this constraint. Do the below steps:
create a height constarint for your view in your interface.
Then add an IBOutlet object in your class for this constraint. for example,
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
Connect this object in your connection panel.
Then change the value of this constraint whenever you needed
self.heightConstraint.constant = 40;
One more thing, you have to call [self.view layoutIfNeeded]; method once you changne the constraints.
Enjoy :)
You can find out constraint like that
extension UIView {
var heightConstaint: NSLayoutConstraint? {
get {
for constraint: NSLayoutConstraint in constraints {
if constraint.firstAttribute == .height {
if constraint.relation == .equal {
return constraint
}
}
}
return nil
}
set{
setNeedsLayout()
}
}
}

iOS: Set View Size in Code with Autolayout enabled

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.

Resources