How to resize my view programmatically using autolayout - ios

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

Related

How to set hide and show view using constraints like auto layout /StackView

I have a larger screen to design, it had plus button, for adding more fields in it. For example, i need to add 2 Email/Phone number, when i press plus button it will show one more textfield with in Email header as vice versa. If i use hide and show method programmatically, the rest of the space will be blank. For this i need to set constraint and setLayout for view. How is it possible?
I would like to give idea so quick look at it.
Take a scroll view inside take one view that having all field your textfield like email and more text that you want.
Now design all field whatever you have to show.
add
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *ViewHeightConstraints;
for container view when you hide textfield just subtract height of all hideTextfield to ViewHeightConstraints.
ok when you hide textfield do something like this:
ViewHeightConstraints.constant = 0;
_addressView.hidden = YES;
[self.view layoutIfNeeded];
ok when you Show textfield do something like this:
ViewHeightConstraints.constant = 243;
_addressView.hidden = NO;
[self.view layoutIfNeeded];
whenever you change constraints write this Everytime.
[self.view layoutIfNeeded];
for setting frame for your main View Set height constraints for MainView. i have done like this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *containerHeightConstraints;
_containerHeightConstraints.constant = _bottomView.frame.origin.y+_bottomViewHeight.constant;
[self.view layoutIfNeeded];
Hope it will work.
You need to take a IBOutlet of the field height constraint for this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *fieldConstraintHeight;
Now when you want to hide it use
fieldConstraintHeight.constant = 0;

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 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;

UIScrollView in Interface Builder not working

I originally had a normal view with a bunch of stuff in it, and I want that view to be an UIScrollView, so I created a new ScrollView and dragged everything over. The view is not scrolling.
After looking around - I unchecked use autolayout, but that didn't work. I also realize that this could be solved by setting contentSize, but I have access to this view through a variable that is of type UIView, and not UIScrollView. In other words I would be doing -
self.someController.view.contentSize = //something
where self.someController.view is only an UIView and contentSize is not a property of UIView(or at least that's what I'm seeing- I get compiler warnings).
Why is this scroll view not scrolling?
EDIT -
So I stupidly forgot I can cast - I did that, but it's still not working -
UIScrollView* basicCardView = ((UIScrollView *)self.detailController.view);
basicCardView.contentSize = CGSizeMake(basicCardView.frame.size.width * 12,
basicCardView.frame.size.height);
You need to connect your scrollview to Iboutlet var in .m .
Create var in your interface:
IBOutlet UIScrollView *scrollview;
And connect to your scrollview in storyboard, then you can set contentsize.
[scrollview setContentSize:CGSizeMake(2000,200)];
Elto has it correct. To elaborate: In your view controller .m say it's called MyViewController:
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
Go back to storyboard, select your view controller and select the connections tab (last one on the upper right). You should now see an outlet called scrollView. Drag a connection from that outlet to the scroll view that you added to the scene.
Now the content size code can be written. Just to get it working, use large constants in content size, like:
self.scrollView.contentSize = CGSizeMake(1000, 1000);
The size you're setting in the post (on the wrong view) looks like it's just the size of one of the subviews. You want the content size of the scroll view to be more like a multiple of the scrollView's size. You want the rectangle it can scroll within to at least encompass the frame (not the bounds) of every subview it contains.

Resources