Missing constraint for connecting view.top with dynamic height uilabel bottom - ios

I am trying to arrange 3 views - 1. Image view with fixed height and width 2. Textview with fixed width but dynamic height and 3. Tableview with fixed width and height adjusted according to available space after textview.
What constraints I am missing here. Why I need to give Textview or Tableviews Y pos or height constraint. Doesn't it make their height fixed?

You have to put a height constant to your textview. Because autolayout can't figure out how many pixels to give to your both view.
If you want to adapt your height depending on the text, you can use :
TextViewHeightConstraint.constant = [TextView intrinsicContentSize].height
into your code after your link your constraint via iboutlet.

Related

How to set autolayout constraint so that the height of an UIImageView is equal to the height of the superview?

I have created a UIView that consists of 3 UILabels and 1 UIImageView as below -
The UILabels have lines set to 0 so that the height of the UILabels change dynamically
I have added constraints such that the height of the image depends on the height of the UILabels as shown below -
I have set width of the UIImageView to a constant of 100.
I would like to set a constraint on UIImageView such that the height of the UIImageView is equal to the height of the whole view.
I tried adding a "Equal Heights" constraint on the UIImageView and the whole view. I observed that the height of the whole view depends on the height of the UIImageView and not that the height of the UIImageView depends on the whole view as I want it to be.
Can anyone point out how I can add a constraint to the UIImageView such that its height is equal to its superview but the height of the superview is not dependent on the UIImageView?
Edit -
The below image shows the problem that I am facing. When an image is set to the UIImageView, the height of the view changes.
I want the height of the UIImageView to be equal to the height of the view, but I do not want the height of the view to be dependent on height of the UIImageView
Looks like you need to reduce the vertical content compression resistance on the image view.
Set it to something very low like 100 and try again.
I believe you haven't add the height constraint to the superview. Setup height constraint for the superview and then add a "Equal Heights" constraint on the UIImageView and the whole view.
In fact default compression resistance of your UIImageView is 750, while content hugging of root UIView is 250. This means AutoLayout engine layouts everything correctly. You can change vertical compression resistance to value lower then 250 (UILayoutPriority.defaultLow - 1) to achieve desired layout.

My scrollView does not wants to scroll

This is my setup:
I do not know what I am doing wrong. The image view is bigger than the size of the view and of the scroll view. The constrains are set al followed:
Scroll view: equal heights to View * 0,5, equal width to View, center Y and X to View.
View (inside Scroll view): pinned all zero's inside Scroll view, equal heights and width. I also tried instead of equal heights and widths to center X and Y inside Scroll view, but it won't scroll.
How can I let the Scroll view scroll? Thank you.
Add a leading, trailing and top constraint and equal height of UIScrollView to superview with 0.5 multiplier. Now to your contentView (the UIScrollView subview), add a leading, trailing , top and bottom constraint. Also add equal height and width to UIScrollView. Set the height to a priority of 250. Add constraints for UIImageView inside this contentView.
Since the contentView will have a fixed height of low priority equal to the UIScrollView height. This fixed height constraint will break once the UIImageView total height(based on the constraints you add) will get larger than the UIScrollView height and the content will become scrollable. So at the very least you will always have a view half the screen size and become scrollable once the content becomes too large vertically.
You need to give contentSize to scrollview.
ScrollView.contentSize = CGSize(width: 1000, height: 500)
Which constrains have you given to imageview?
set constraint of imageview:
Trailing ,leading,top,bottom - 0 and also give height constraint.

set dynamic equal height of two uiview depends on their subview using autolayout?

I have one superview in that view there are two subview.
I have set equal height of that two view. Those subview height changing respect to label height in it.
This is first screen that i have implemented. It works when label 2 height is greater then label 1 height changes with respect to label 2 height.
This is first screen. It work properly but another situation it is not working when label 1 height is greater than label 2 height then it's not working expected.
output like following screen
As expected label 2 need to adjust with respect label 1 but then also label 1 is adjusted with respect to label 2.
so how to apply constraint for set equal height for two view depends on their subview height.
You can make your label heigh dynamically
Call a method for UILabel height
-(CGSize) getContentSize_Label:(UILabel*) myLabelView{
return [myLabelView sizeThatFits:CGSizeMake(myLabelView.frame.size.width, FLT_MAX)];
}
and set label heigh like
myLabelView.frame = CGRectMake(myLabelView.frame.origin.x, myLabelView.frame.origin.y, myLabelView.frame.size.width, [self getContentSize_Label:myLabelView].height);
and set
myLabelView.numberOfLines = 0;
What you are missing is most probably equal height constraint.
Select two container views of the labels and then go to Xcode/Editor/Pin/ and select Height equally.
I've just checked it and its working for me.

How to make this auto layout arrangement work with scrollview

I have a screen layout where there are two resizable labels , which will contain multiline text. These labels are placed inside their parent views which intern are added to main contentView, main contentView is then added to scrollView ( thats what most of the solutions suggests). For both the labels (below About and Time and location labels in first image attached) I have set height constraints as "greater than or equal to" and setting the numberOfLines to 0 as well as calling SizetoFit, but actual output is not as expected (see second image attached). There are no constraints warnings. All constraints are provided for all the elements.
The code in viewDidLoad is as follows for one of the label.
self.lblAbout.text = #"this is a long two three lines about string which will have two lines this is a long two three lines about string which will have two lines";
self.lblAbout.numberOfLines = 0;
[self.lblAbout sizeToFit];
[self.lblAbout setPreferredMaxLayoutWidth:244.0];
Also
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
scrollView.contentSize = contentView.frame.size;
}
Not if any additional constraints are needed, I have added all leading , trailing , top , bottom constrains along with height wherever needed, plus the spacing between all the views is in place.
What i want is the labels should get adjusted to number of lines and the contentView (parent view) should scroll inside ScrollView as the total height will be larger than the screen available.
*** problem I think is the outer view of the labels aren't getting resized as per the label because of which all the views below it aren't getting repositioned ****
Please try this Solution,
1. add Height Constraint to superView of your Label.
2. add IBOutlet of that Constraint
3. add this Method to find out Height of your Text
you need to give width of super view of your Label so width will be same as your super view
4. Now get Height form returned CGRect and assign it to your Constraints's constant. it should be like
heightConstraint.constant = youObject.size.height;
please Make sure you have added other Constraints accordingly this. if not than you need to also increase Height of other superviews accordingly.
(CGRect)sizeOfDetailLabelFromString:(NSString*)string maxWidth:(CGFloat)maxWidth{
NSDictionary *attributes = #{NSFontAttributeName:FONT_LIGHT};
CGRect rect = [string boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil];
return rect;
}
Here is what worked finally.
Added ScrollView in main View
Added View as a contentView ( be sure to rename it in designer to something than just view )
pinned scrollview to main view using leading trailing top and bottom space constraints.
Pinned contentView to scrollview same as above
Added all the components with their respective constraints
Set the height of UILabel which i want to resize using "Greater than or equal to constraint (this is necessary) and set number of lines to 0 in code
The parent view of label shouldn't have any fixed height but enough constraints to calculate it at runtime.
Make sure ScrollView has no ambiguity in calculating contentSize.
imp - Add constraints to width of Main view , scrollview , contentView ( that was in my case , you may not need equal width constraint between contentView and scrollview , but between contentView and main view its necessary)
Now scrollview scrolls exactly the way it's needed.
To my belief most of the above things i already did but somehow it wasn't working ,deleted everything and did all the things again few times and it worked.

iOS how to get a UILabel view's height programmatically

I am pretty new to iOS dev. I am trying to create a view with a UILabel and a UITableView. The UILabel is used to display some header text & the tableview is positioned below the header & it covers the remaining portion of the screen.
I am creating this view using code & not the storyboard approach. So I need to specify the height & width of my tableview as constraints. For the width I have set a constraint where it matches the width of the container view. However for the height I wish to use the following equation:
tableview's height = container view's height - UILabel's height
For this I need to know how I can get the container's & the UILabel's height via code. I tried the following approach-
CGFloat header_height=lblHeader.bounds.size.height;
But this gives the same height as the container view's height .i.e. 460. Can someone please explain how I can get the height of the UIlabel view?
To get the height of the UILabel do this instead:
CGFloat header_height = lblHeader.frame.size.height;
Frames and bounds are not the same.
Cocoa: What's the difference between the frame and the bounds?

Resources