I have 2 views(say topView and bottomView) in my UIViewController with topView at top. Both views grow with aspect ratio constraint. topView and bottomView has 0 vertical space between them. I want to hide bottomView in a particular case. But problem is that I don't have height constraint available for it. I only have Vertical Space constraint between topView and bottomView. So I decided to move bottomView under topView. I tried to do following in view controller's viewDidLayoutSubViews
self.verticalSpaceConstraint.constant = -1*[bottomView intrinsicContentSize].height;
and
self.verticalSpaceConstraint.constant = -1*[bottomView bounds].size.height;
The intrinsicSize always returns (1,1) and bounds always returns (0,0,320,568). How can I achieve this it?
You can add height constraint (with constant value 0) and aspect ratio constraint both at a same time to the view, but these constrains should have different priorities and non of these with priority 1000 (required). Then to hide view just set programmatically priority of aspect ratio constraint lower then height constraint or vice versa to show it. Hope this helps
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.
I'm using a scrollView and inside it used a view.
I set sizes like this:
I think the height of my view always is 959.
but when I change the height from 959 to 2000, I see a orange line and when I use Editor->resolve Auto..->update frames my custom hight returns to 959.
even when I set height from pin I see red lines:
I want all my content scrolling. The content's height is 2000.
You check this following way:
Scrollview Constraints:
Set top to your super view
Set bottom to your super view
Set leading to your super view
Set trailling to your super view
Take One UIView into scrollview and apply following constraints(Equal to same Scrollview width and Height)
Set top to scrollview
Set bottom to scrollview
Set leading to scrollview
Set trailling to scrollview
Dont forget to set Equal width to scrollview
Then do add your all UIElements whatever you want.
Consider like below:
lable 1----> set necessory constraint to UIView
lable 2----> set necessory constraint to UIView
lable 3----> set necessory constraint to UIView
lable 4----> set necessory constraint to UIView
lable 5----> set necessory constraint to UIView and you give Bottom constraint to UIView
Then scroll will automatically work..Dont adjust value like that.If you want more large screen Change View controller Simulated size to Freeform and there you adjust your prefer height and place scrollview based on that.
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.
I have this view hierarchy
I want my content to grow vertically in content view but not horizontally. so I want to constrain ContentView's width to be the same as device's width. How can I do this in Interface Builder?
Note: The image's width in Image View is bigger than device's width, so right now ContentView's size grows both vertically and horizontally
just add the constraints to content view as leading space,trailing space,top space,bottom Space along with fix content view width constraint as constant and then create the outlet for width constraint value and in view controller side write method as
-(void)viewDidLayoutSubViews
{
_scrollViewContentViewWidthConstraint.constant = self.view.frame.size.width;
}
I have a UIImageView object which has constraints "equal width" and "equal height" to "superview" (ViewController's main view).
When I debugging, imageView's width and height isn't equal with ViewController's width and height. Am I missing something ?
The constraints are applied somewhere between after viewDidAppear and before viewDidLayoutSubviews. It is really frustrating as you don't have access to the correct size until the view is already visible to the user.
what you can do to cover the hole view is to add to the imageView trailing, leading, top and bottom constraints = 0 and remove the height and width constrains.
hope it helps.