It gives me the following warning: Need constraints for: Y Position or height. I can get it working with for example TextView, by disabling "Enable Scrolling" but ImageView does not have a setting for something like that. I've also tried selecting my view controller and enabling/disabling "Adjust Scroll View Insets" which gives no results.
EDIT Image of my hierarchy.
The layout is ambiguous, because AutoLayout does not know the height of your UIImageView instance. Constraining views against a UIScrollView is somewhat special, because the ScrollView's contentView needs to calculate it's size based on the subviews. To to this AutoLayout needs to be able to calculate the dimensions of all the subviews without relying on the ScrollView's size.
There are different ways to solve this.
Add a explicit height constraint to the imageView
Add a aspect ratio constraint to the imageView
Set a placeholder intrinsic content size in the Interface Builder
Related
I´m setting up my view programmatically to show up when a button is pressed. My question is what's the difference between positioning a View programmatically with CGRect or with Constraints in storyboard/programmatic constraints?
I tried to set it programmatically and it works fine.
Simplified:
With CGRect you give certain values and UIView will have size and position depending on these values
With constraints you give instructions how these values should be created
CGRect will not fit all screen sizes as Constraints will. for example. CGRect x: 100, y: 500 may fit a large screen, but for an iPhone SE it will not work because the Item will be off screen. however with constraints like maybe Centre X & Y with a width and height constraint will centre something in the centre of the screen on all devices.
I have a button inside a horizontal stack view and am trying to give it a border using the Runtime Attributes in Xcode. Here is what my storyboard looks like:
When I run my app the text has no padding so the border fits very tight:
Is there any way to make the border of the button fit a bit looser? I can't seem to make the UIButton bigger since it's in a stack view...
you can look at lowering the content hugging priority of the add button as both are UIButtons and have same compression resistance and content hugging priority and stack view algorithm in such cases stretches the leftmost view.
You have to set a width constraint on the button.
Hi I'm trying to understand auto layout and have a simple question. Correct me if I am wrong, but unlike labels and buttons, image views/views do not have an intrinsic content size which makes working with them in auto layout a tad bit harder. I just want to know what constraints I would need in the examples below so they would show up appropriately in both portrait and landscape, if I were to be working in the W:Any H:Any size class.
In the case of the image view I was also wondering if the outcome would change if I were to apply the constraints and then the image.
https://s15.postimg.org/s5814ct3f/Screen_Shot_2016_09_03_at_6_20_22_PM.png
https://s9.postimg.org/jxdeud13j/Screen_Shot_2016_09_03_at_6_20_56_PM.png
Thanks!
You can apply this:
1) Select an imageView, and choose following 4 constraints:
top leading contraint
width constraint
aspect ratio constraint (so it is going to be dependent on its width and it won't be distorted)
horizontal center in container constraint
If you are likely to need to change the image size after you've implemented the autolayout, you may need to change constraints. Thye are responsible for handling items and their sizes, rather than then dimensions (width, height) that are in size inspector.
I made a little animation to see, how to change constraint in interface builder, while you are designing your scene:
This is a very simple ViewController. I have an imageView inside a scrollView. Using autolayout I set scrollview's constraints to its view's edges. Also, imageView mode is AspectFit.
The image is provided at runtime, and it may be a landscape or portrait size. When I run the app, the image doesn't fit the current screen size. It seems that scrollView's boundaries are not being honored, thus only part of the image is being displayed. It used to work on ios7 and xcode5, but it's broken for ios8 xcode6.
Any ideas? I need to keep the use of autolayout whenever possible.
Your going to set the imageviews horizontal constraints and pin them to self.view (the scrollviews parent view) instead of the scrollview.
There is a good example of this here:
Using UIScrollView with Auto Layout in iOS
Try this to zoom out completely
[self.scrollView zoomToRect:self.imageView.frame animated:YES];
Have you tried a call to imageView.sizeToFit() in viewDidLoad? My impression is that imageView's size is fluid until the outlets are set. A call to sizeToFit forces the issue.
I want to set the content view of a scrollview to whatever the current screen size is, but AutoLayout is doing some funky stuff. This is trivial in code... just create a scrollview with a frame that is the superview's bounds. Then create a content view with the scrollview bounds and populate it and set the alwaysBounces... properties to YES. In interface builder though, this is some kind of sinful thing it seems.
I know Autolayout handles scrollviews totally differently because it wants to infer the content size based on constraints. My approach that failed is setting the scrollview to have 0 distance to its superview (all sides attached). Then, the same with the content view (the single scrollView subview)- attached to all superview edges. Then the precompiler thing complains about not knowing the content size, so I set a width and height constraint at placeholder to be removed at build time. But the result is a (CGRect){0,0,0,0} contentView. The 0 space to trailing edge and bottom of superview are totally ignored.
So how can I make a scrollview with a dynamic content size based on the screen size?
Bonus points if you can explain how you would do the same, but for a content size of 2x screen width.
You can try setting a constraint for the content view's width to equal the scroll view's width (for your bonus question: with a multiplier of 2). Same for height.
Not sure what the point of having a scroll view whose content is always the size of the scroll view is. By definition it wouldn't scroll, right?