iOS 7 Autolayout doesn't work - ios

I've been working today on this problem for a while now and i couldn't solve it. I downloaded Apple's Autolayout Guide and couldn't find an answer/solution. The problem is that I'm adding a UIImageView to my View Controller and when I change the windows form factor from 3.5 to 4 and vise versa, the image view is stretched.
To solve this, I tried many ways such as clicking on reset to suggested constraints, clear all constraints, and build my own constraints by pinning and aligning.
By aligning and pinning I set the height and width to the size as in the Interface Builder and the horizontal and vertical positions as well. Unfortunately, the image still stretches upon changing the form factor in Interface Builder. I tried to only pin the height of the UIImageView but it doesn't work.
How can i work this out?

Autolayout only deals with the frame/bounds of the UIImageView. The actual way the image is rendered is controlled via it's contentMode property. You need to set it to either UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill to avoid stretching.

Related

Autolayout UIImageview Swift

So,
I am trying to build an app from design, the design consists of 3 images horizontally on a portrait layout
Supporting Iphone 5 and upwards.
From this guide http://iosdesign.ivomynttinen.com/ the Iphone 5 should have 640px of horizontal space.
Each image is 100px by 148px high, therefore I have created added 3x UIImageview and laid them out horizontally.
Now without any constraints I get the following
If I auto constrain I get the following, same if I add constraints manually
Skips out the middle.
If I go to any any constraint , I get an error message in the logger.
This can be a vertical space to top layout constraint, just the one and it breaks.
I assume as images are only 100px wide, I should have plenty of room.
On the UIImageview I have done the following
Set Height and Width
Content is AspectFit
Clip Subviews Selected
Auto re-size selected
I have either not understood App design and the images do not fit, or have done some incorrectly with IB and UIImageview?
Any thoughts on this.
Sorry not a lot of code as it's all Interface builder.
Thanks
Sorry,
My own fault, "reset to auto constraints" and "clear constraints" had a bit of an issue, there was a bunch of constraints still kicking about when I delved in deeper.
In the end deleted then all and then manually added.

Image Height Constraint Not Working

I have a UIImageView and used autolayout to add height and width constraints (and other ones as well). There are no conflicting constraints or warnings. I am querying an image from a database and setting the imageview's image to that image. However, the image is showing up with a much larger height than I have specified via constraints. How do I fix this?
This line should resolve your problem :
yourImageView.clipsToBounds = true
You can do it in Storyboard by checking the "Clip Subviews" of your UIImageView element.
1
YourImageView.contentMode=UIViewContentModeScaleAspectFit;
May be the problem is coming because of your contentMode ,so please your imageView content mode.
2.if you are developing application for a single phone then use aspect fit for that[constraints]
If you are getting image from server then you should resize the image according to your imageView size ...its a best practice.

ios - UIScrollview w/ autolayout on Xcode 6

I have been struggling for days with this implementation, and even though I have tried to do every tutorial I found on the web, I still cannot make things work the way I want.
Basically, I am trying to put my login form in a scrollview, so that it takes the whole screen at first (and on all iPhones / iPads), and if the keyboard appears everything should move. The problem IS, my view doesn't take the whole screen... Either it is too large, or too high, even though in Interface Builder everything seams correct (from layout to constraints). Below and image of the layout I want to achieve (I am using an universal storyboard, with Size Classes and Autolayout enabled):
http://img4.hostingpics.net/pics/829115app.png
Can someone point me out on achieving this layout ?
Thanks in advance.
I would suggest pinning top, leading and trailing spaces of your scroll view to its superview. And set a bottom space constraint less or equal to the keyboard's height if you set it to 0, the scroll view won't be able to resize.
With your form layout set vertical center constraints and top space to superview constraints for your top label being more or equal than the distance you set in the IB, and then you can set relative space constraints between each of the components.
Hope I answered your question.
Edit: Just the provided project and got it working. I think the problem is caused by it being a containerView inside a scrollView. And both the container and the scrollViews content view adapt to the size of its subviews. Because of that, setting relative constraints won't help.
What I did was to set an explicit size (screen's size) to the containerView and setting setTranslatesAutoresizingMaskIntoConstraints(true) to it.
I modified your project and uploaded it here

Resizing a UITextView in UIScrollView using Auto Layout issue

I really hate to ask here because I usually try to figure things out on my own. But on this one I've stuck for days and can't find a solution anywhere online.
I have a ScrollView containing multiple subviews. I've got an image view and two labels at the top with fixed heights. Then there is a UITextView and another ImageView (see pictures).
I add the text to the text view programmatically so it should have a dynamic height and the ImageView should move to the bottom so you can scroll. I don't want the TextView to be scrollable in itself but I want all the subviews to move as well.
I know I should be able to solve this issue using constraints. But I feel like I've tried everything and nothing worked yet. It worked when I disabled auto layout and moved the views manually. I'm wondering if there is a better way though.
As you can see I pinned the TextView to the ImageView above with a 1,000 priority and to the ImageView below with a 1,000 priority. The height constraint can not be deleted so I set it to the lowest possible priority. The ImageView on the bottom is pinned to the bottom of the superview with an absolute height. Its height constraint also has low priority. (I can post an image of the ImageView's constraints, if it helps)
I also tried adapting the frame programmatically but this is not working well in combination with auto layout. (If it helps I can of course post the code)
What am I doing wrong? Shall I just disable auto layout and do it manually? This seems unclean to me. Is it even possible to do?
I really appreciate your help :)
Greets,
Jan
Make sure the Scrolling Enabled attribute on the UITextView is unchecked in Interface Builder. I believe that the Auto Layout system takes that into account when it calculates the intrinsic content size.
If somebody is struggling with a similar problem: This is what I ended up doing:
Remove all subviews from the ScrollView in IB
Programmatically add a single UIView to the ScrollView.
Add all the views to the UIView as subviews (move them using setFrame)
Set the Frame of the UIView appropriately to the subviews
Set the ScrollView's contentSize to the size of the UIView.
A little more work but it finally works. This follows Apple's mixed approach guidelines that can be seen here (look for UIScrollView): http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html
The problem is the height setting. You somehow have to try to delete it. If you have added other constraints that are "sufficient", it should become deletable.
At the moment you have one user constraint for the height that is "Greater or equal" and an "Equals" constraint as well. Clearly, those are not working well together.
Maybe there is a conceptual error as well. The lower image view should not be fixed in position, so the distance to the lower image view will not be a "sufficient" constraint to let you delete the fixed height.
I think it should work if
the lower image view has a fixed height and
a fixed distance to the text view above, and
the text view has a minimum height as well as
a fixed distance to the image view above
(which should be fixed in relation to the superview).

How to prevent UIImageView from resizing with Autolayout

I'm recreating my UI with auto layout and have a navbar across the top and a 2x2 grid of UIImageViews filling the rest of the screen. I put in all the constraints so it maintains this arrangement in any orientation and it's working well when the UIImageViews don't have an image in them. When I programmatically load an image in the UIImageViews, they change size and placement even though I have constraints that should prevent this.
I have all the autoresizing masks off for everything and the UIImageViews set to Aspect fit.
This autolayout stuff is new to me, but I thought I had it working...until I put an image in there. Why would that change the size and placement of the UIImageViews?
Thanks for the help!
You can modify contraints at runtime if you link them with an outlet in your view controller.
For your case you would add a width and height constraint and link them to two outlets.
Then after you know the image dimensions you would change the constant value.
I am describing this in detail for a similar use case I had here: http://www.cocoanetics.com/2012/11/first-good-auto-layout-use-case/

Resources