Frame Resizing using AutoLayout - ios

I have an UIView inside which i have a UIButton. I am using auto layout. Now I want to reduce the height of the UIView. How should I do it ? On reducing the height of the UIView , the UIButton inside the view should also move up at the same time. How can i do this using Auto Layout ?

You can create IBOutlet for height constraint of you view and you can change value for it.Just check how to create.
Now use the code below to set height of you UIVIew
heightConstraintOfView.constant = DYNAMIC_HEIGHT;

Related

iOS:use the frame constrained by Auto Layout to create a subview

I create a view called boundView using IB and Auto Layout,then in the controller I call [self.boundView layoutIfNeeded],then I pass self.boundView.frame.size to a method to generate the size of boundView's subview CardView. And then use the
PlayingCardView *playingCardView = [[PlayingCardView alloc]initWithFrame:frame];
to create subview programmatically. I do use NSLog to check that the size of subview is smaller than superview. But when I use [self.boundView addSubview:CardView] to add the subview. It is larger than superview!
Is there something wrong with the coordinate?Or it is because I combine the Auto Layout with the view I create by code?
Where are you doing this? If it is in viewDidLoad then the autolayout sizes will not have been calculated yet. Try doing it in viewDidLayoutSubviews.

what should be the height of uiscrollview and main view that i can set after adding other views to uiscrollview

After so much trouble i am able to scroll the scrollview, and it is working fine. Now my question is how to set the dynamic height of scrollview and main view after adding view inside scroll view. As adding height from IB make it static and inside content height may very.
steps that i followed are.
Added a uiview controller inside my storyboard.
Then inside this uiview added one uiscrollview.
Set the height of the main view to 1000 and scrollview to 1000.
Selected the uiscrollview and reset it to suggested constraint.
In this uiscrollview i added many labels, uiviews, textviews, etc programatically.
Now the main isuue is this 1000 height that i set in step 3 is static and the content is dynamic, so how to set height dynamically. and also is it necessary to follow step 4, when i am doing it programatically.
If you know the new subview's height, you can change the frame size by
// Add your subview to scroll view
...
// Set new height
CGRect newScrollFrame;
newScrollFrame.origin = MyScrollView.frame.origin;
newScrollFrame.size = CGSizeMake(MyScrollView.frame.size.width, MyScrollView.frame.size.height + MyNewSubView.frame.size.height);
MyScrollView.frame = newScrollFrame;

Update constraints of other elements when UITableView height change

I have a UITableView of which I am defining height as 200 in autolayout. Based on that I am laying out other elements below it like UITextField etc. After that in run time I am fetching data from server and populating in UITableView due to which i am updating UITableView's height based on its content size. Following code I am using for it
self.myTableView.frame = CGRectMake(0 , 0, self.myTableView.frame.width, self.myTableView.contentSize.height)
But due to this, all the elements placed below UITableView still appear at same location which they were while laying out in Autolayout. Means change in height of UITableView makes no difference to them. Following image depicts this problem. What could be possible solution for this?
Here you can see, text fields are getting overlapped on tableview at run time. I am using Swift 2 in Xcode 7.2
If you have all required constraints to your table view and other view.
Don't change the frame of TableView to change height of it.
Instead create IBOutlet of height constraint of your tablview.
e.g. say IBOutlet name is constraintTableViewHeight,
then you can the the hight easily.
constraintTableViewHeight.constant = yourNewHeightValue
//update all constraint of your view and its inner view
self.view.layoutIfNeeded();
Refer Image to create IBOutlet for your height Constraint.
Take IBOutlet of NSLayoutConstraint for tableView Height and set its value not set tableview frame it's not working if you are already given constraints for tableview so change tableview hight constant
like if you take tblHeight for tableView height then set tblHeight.constant = self.myTableView.contentSize.height
You have to create an outlet connection for your table view height constraint:
#IBOutlet var heightConstraint: NSLayoutConstraint!
and when you want to change table view height, you can change the constraint value like this:
heightConstraint.constant = newHeightValue
self.view.layoutIfNeeded()

How to make a UIView expand to contain a UIImageView or a UITextView with auto layout and IB?

I have a UIView that contains two subviews - a UIImageView or a UITextView. The UIImageView has a fixed height and width.
The UITextView has variable size.
Only one of the UIImageView and UITextView would be displayed at a time. I plan to accomplish this programmatically by setting hidden = YES.
I would like the UIView to hug whichever child view is not hidden with no margin.
I would like to accomplish this with IB and autolayout if possible because the rest of the view is built this way.
So far I have created constraints that link the 4 edges of both of the subviews to the parent view and constraints for the height and width of the UIImageView. Naturally this creates a content priority ambiguity.
I would appreciate any advice.
Thank you!
So the contain view has to have trailing constraint and constraint with bottom which should have IBOutlet(s) references in viewcontroller and then when the text changes :
- (void)textViewDidChange:(UITextView *)textView {
[self.textView sizeToFit];
self.containerViewTrailingConstraint.constant = self.textView.contentSize.width;
self.containerViewConstraintWithBottom.constant = self.textView.contentSize.height;
[self.view layoutIfNeeded];
}
And when the textView is hidden then you have to set the self.containerViewTrailingConstraint.constant, self.containerViewConstraintWithBottom.constant in relation with the imageView

UIView's height depending on the height of the UIWebView inside

I currently have a UIScrollView with multiple UIView inside. And inside each UIView I want to put a UIWebView. The height of each UIWebView is set dynamically depending on the content. How could I do now to set the height of each UIView depending on the size of UIWebView ?
Thanks
Try something along the lines of:
uiView.frame = CGRectMake(desiredX, desiredY, desiredWidth, uiWebView.frame.size.height)
You should use auto-layout and align the bottom of the UIWebView with the UIView.
[uiView alignBottomEdgeWithView:uiWebView predicate:#"10"];
Example: http://code.dblock.org/ios-uiview-with-an-image-and-text-with-dynamic-height

Resources