I am having an imageView and a label side by side. I want to remove width and height of the image when it's hidden. Yes we can do it programmatically by setting the height and width constraints to 0, but is it any other way by storyboard. I have placed imageView inside horizontal stackView without width constraints but it is working just opposite, increasing it's width on hiding the imageView.
How about use another UI for shrinking ImageView
for example
AS IS
UIStackView with arranged UILabel, UIImageView, UILabel, UIView(isHidden true)
when you need to shrink UIImageView with hidden then make hidden UIImageView and make shown UIView.
It makes UIImageView width 0 like because UIView should be expanded full size (hugging priority less)
TO BE
UIStackview with UILabel, UILabel, stretched UIView(low hugging priority and isHidden false)
Related
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.
How can I get a UILabel with a variable number of lines to push down the content in a UIScrollView?
I currently have a UIScrollView pinned to the superview, a container inside pinned to scrollview and equal height/width to main view, and this blue UIView (height 50), 2 UILabels and a UIImageView (height 200) inside the container.
In landscape I want the UIImageView (i.e. black box of height 200) to not be pinned to the bottom of the superview, but be offscreen and the scrollview content size to have been adjusted.
In portrait I want the labels to size to fit and the black box to respect the top constraint to the bottom label:
Tried more stuff and the following worked:
set both labels' vertical resistance to 1000
delete the "equal height" constraint on the container
So the solution to seems to be:
add a scrollview and pin all edges to vc's view
add container to scrollview, pin all edges to scrollview, equal width to vc's view (height varies based on bottom pinning to content), add all content to container
I'm having this weird issue with my constraints which causes the UILabel (Caption Label) to be a fixed height instead of dynamically changing height depending on the text.
I have a view (Vertical View) with a top constraint on the label above it. The Vertical View contains a view (called View) which I'm using as a divider that is centered from top to bottom with a width of 1. On the left of the divider is a UIImageView (Left Image View) with constraints leading, top, bottom equal to superview and trailing equal to View. I want to do the exact same thing to the UIImageView on the right of the divider but here is where my issue comes up.
If I use a fixed height as seen below, the UILabel above Vertical View dynamically changes its height like I want but this is obviously not how I want the UIImageView on the right to appear. I want it to be similar to the UIImageView on the left of the divider with equal height and width.
If I set the top constraint of the UIImageView on the right to the superview Vertical View, similar to the UIImageView on the left of the divider, the UILabel above Vertical View doesn't dynamically change height anymore. The UILabel now has a fixed height which I believe comes from the fact that UILabel has a height of >= 14.
How can I properly set the constraints so that I can have both UIImageViews next to each other with equal and height contained within the Vertical View and still have the UILabel above Vertical View dynamically change height depending on the text that I set the UILabel to?
On the RightImageView, you first need to get rid of the "Height = 50" constraint. This is what is causing it to be small.
Next, if that alone doesn't fix you, can you try setting the following constraints instead of using the superview for the constrains (instead make it mirror the LeftImageView):
Left: Leading spacing to divider view
Top: Align top edges to LeftImageView
Right: Horizontal space to superview (your vertical container view)
Bottom: Align bottom edges to LeftImageView
This should allow the views to remain the same height and width (assuming your distances between left/right edge of vertical container view are the same, and the distances between divider are the same).
Now, ensure the size constraint for width of the divider is set to 1 and not >= 1. Also, ensure the vertical container view has a Compression lower than the Label.
One final note--your screenshot shows the result that IB is showing you (with the dotted yellow box) on the LeftImageView. One you update your constraints correctly, this yellow box should go away.
Regarding the UILabel - if you want this to grow dynamically, you need to do the following:
myUILabel.numberOfLines = 0;
myUILabel.text = #"Enter large amount of text here";
[myUILabel sizeToFit];
I have a ScrollView embedded in my ViewController. Inside the ScrollView I have embedded a content view (UIView) which has a UIImage set to match the left, top and right of the ScrollView with a dynamic height that changes depending on the aspect ratio of an image that the user can load after the ViewController loads. There are three buttons all horizontally aligned in the content view and spaced evenly apart from each other.
When the user loads in a photo that is too big for the screen it should just resize the ScrollView and the content view appropriately to allow scrolling to see the buttons but instead it just bunches up the buttons at the bottom of the screen.
Here is how the buttons should look:
Here is what happens when the photo is too big:
Here are the constraints of the ScrollView:
Here is my resizing code:
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
let multiplyNum = screenSize.width / img.size.width
imageViewHeightConstraint.constant = (multiplyNum*img.size.height)
imageView.image = img
Even when I try and change the ScrollViews height programatically to a very large number it still won't get any larger than the ViewController (no scrolling).
Constraints of ContentView:
Constraint of ImageView:
Constraints of first 2 buttons:
Constraints of last button:
Make sure to set all the constraints that completely define the vertical layout of all elements (top constraint for image, vertical space between elements and bottom constraint of last element), and try changing the priority of the content hugging or compression resistance of the elements.
Edit:
You can achieve that behaviour with this view hierarchy:
- UIView
- UIScrollView
- UIImage
- UIButton
- UIButton
- UIButton
There is no necessity to add a container view if you set the constraints like this:
scrollView:
Leading, trailing, top and bottom constraints to view (all 0)
inner views (horizontal):
Leading constraint from imageView to scrollView
Trailing constraint from imageView to scrollView
Equal width from imageView to view
inner views (vertical):
Top constraint from imageView to scrollView
Height constraint of imageView (this constraint constant will change depending on the size of the image)
Horizontal space from imageView to button1
Horizontal space from button1 to button2
Horizontal space from button2 to button3
Bottom constraint from button3 to scrollView
There is no necessity to change the priority of the content hugging or compression resistance of the elements.
I already check that it works in a project.
In my view I have a scrollview.
In my scrollview I have a UILabel.
I use constraints to set my scrollview in my superview and also for my UILabel.
If I set a long text inside my UILabel the scrollview's content extends to the width of my UILabel.
Instead of that, I would like the content view set to maximum width of my screen and the UILabel goes to multilines.
How can I do this ?
Try setting your label's number of line to 0 and preferredMaxLayoutWidth the width you want, then call setNeedsLayout with your main view.