How to configure view width fixed width and variable height with AutoLayout - ios

I have the following setup: a UIView containing a UILabel and a UIButton. The UIButton has fixed dimensions (it doesn't really matter). The UILabel, however, is constrained by the view's bounds.
I want to be able to set the UIView's width and it should automatically resize itself so that its height allows the whole label content to be visible.
I have tried the following:
self.view.frame = CGRectMake(0, 0, desiredWidth, 0);
CGSize fittingSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
self.view.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);
But this changes both the height and the width of the view, not keeping the width at the desired value.
Is there an elegant method to do this?
Thanks in advance for any help?

In AutoLayout, you would need add a width, top, bottom, leading (left) and/or trailing (right) constraints for the view on to the superview and declare properties for at least the top and/or bottom constraints. After adding the aforementioned constraints you could then change the height of the view by adjusting top and/or bottom constraints constant value.
Here is a link to the Auto Layout Guide.
Auto Layout Guide: Introduction

Related

how to resize scroll view after remove subview swift?

I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size

how make the UIView height equal to its subview's additional height, so that it can easily scroll till the end

I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout

Adding subview to scrollview

I'm trying to add a subview to a scrollview I have in my view controller:
let size:CGSize = self.view.bounds.size;
self.scrollview.contentSize.width = size.width
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, self.tableView.frame.origin.y + 130, size.width, size.height), pageMenuOptions: parameters)
self.scrollview.addSubview(pageMenu!.view)
It works to the extent that it adds it in the correct position and height I want it. But for some reason, right now it only expands to about 60% the width of the screen (I need it to be full screen).
Things I've tried
1) Setting it so self.view.frame.width
2) Setting it to the width of another full screen element.
3) Setting it to UIScreen.mainScreen().bounds
I checked the constraints of the scrollview in storyboard and it's configured to be full screen...so I'm not sure why this wont work.
This issue is related to the constraints that need to be set to the scroll view. I have answered a similar question here. Basically you need to specify a constraint for the scroll view's content view's width. See my answer in above link for a detailed description. The problem is that the scrollview adjusts its size to its content view's size even after we provide proper constraints to the scroll view. So we need to specify the constraints of the content view of the scroll view with respect to the scrollview and its superview so that the content in the scrollview fits our requirement.
One thing I noticed was that in your CGRectMake code you are specifying your y origin to be tableViews y value + 130. That seems like the problem to me.

[Auto layout]Attaching UIView to the bottom of its superview while superview's height is changing

I have a superview which is just a subclass of UIView, and I have a subview which is a subclass of UIImageView that I want to attach to the bottom of the superview.
The height of the subview is fixed, but the height of the superview is subject to change. So basically, I want the subview to always attach to the bottom of the superview while I change the height of the superview programatically.
I'm doing it by Auto layout,
So superview has left,top,right attached to its superview, and a fixed height(I gonna change it programatically), here's its constraints,
And the subview has left, bottom, right attached to the superview, and its fixed height, and another constraint that superview's height should equal or greater than the subview's height.
Finally, when I tap the button at the very bottom, I change the height of the superview. Here's the code,
- (IBAction)testButtonPressed:(id)sender {
[self.dragDownView setFrame:CGRectMake(CGRectGetMinX(self.dragDownView.frame), CGRectGetMinY(self.dragDownView.frame), CGRectGetWidth(self.dragDownView.frame), 100)];
}
However, it's not doing what I wanted, which is keep the subview attached to the bottom of the superview.
Before I change the height:
After I change the height:
As you can see the height of the superview is changed, however the subview still keeps what it was before.
Anyone who can help me?
link constraint:
update view's frame like this:
self.heightConstraint.constant = 320;
[self.view layoutIfNeeded];
As you are using Autolayout Constraint you should update Constraint instead of Frame.
Link Height constraint of your Superview to IBOutlet as #mistyhua suggested.
Now when you what to change Height of your Superview then instead of changing frame of Superview change Height Constraint value.
self.heightConstraint.constant = 320;
[self.view layoutIfNeeded];

Programmatically set size of UIView from Storyboard

I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);

Resources