UIImageView overflowing custom tableview cell - ios

I have a custom table view cell with a UIImage on the left, and two labels on the right one on-top of the other. I originally had only the image and one label, and the image was fitting perfectly inside the cell, but after I added the second label, the table view cell is smaller and doesn't contain the full image(even though the image is still the same size as it was), and doesn't contain the second label. So for some reason after I added the second label the custom tableview cell shrunk in heigh and won't contain them. I have messed with constraints for hours and can't seem to find the right ones. And yes, the image is aspect fit.
Let me know your thoughts, this might be a simple fix, I just simply can't find that fix. Thanks in advance!

When you click on your UIImageView, ensure that it has Mode set to "Scale To Fill" In the attributes inspector. It can be done programatically like this if you want:
newImgThumb.contentMode = .ScaleAspectFit
Also, make sure that your constraints are not conflicting and are properly arranged. If this doesn't work post some screenshots and I will take another crack.

Set mode of image view Aspect Fill, and select Clip Subview in XIB/Storyboard.
Hopefully it works. Thanks

Related

Handle the spacing between elements for a certain condition

I'm building a FeedViewController for my app.
This screen consists in a tableView (or a collectionView), that shows users' posts. A post always have a content, and it can include or not an image.
I was wondering what is the best way to handle the case when there is no image to display, how do you handle the height of the table view cell and assure that the spacing between each item remains the same?
What I've tried :
I created a subclass of table view cell and inside I added some constraints. Within the cellForRowAtIndexPath method of my FeedViewController, I added an "if statement" to check whether the postImageView.image is nil or not, and update the height constraint of the post image accordingly. The problem is, as you can see in the screenshot below, the spacing slightly differs from a cell with an image, to a cell without an image. I would like the spacing between elements to be exactly the same, no matters if there's an image attached to the post or not.
How would you guys solve that problem ?
Thanks for your help,
J.
If you use the autolayout then u need to give bottom and top constraints to buttonlabel. OR if you use the autoresizing then you need to fix the position of buttonlabel. #jellyfish6
I think you have given BottomConstraint for "Bottom Label" that is the reason why it is having different spacings.
I guess instead of giving bottom constraint provide it with top constraint or both and manage it with priority.

UIImageView making other Views inconsistent

I've designed a ViewController with an ImageView, 3 Label-TextField combination, one single Label and one Toolbar at the bottom of the view. All of these view elements are inside a Stack View. The design is like below:
The contentMode property of the ImageView is set to Aspect Fit in the Attributes Inspector.
Now when I run the app and select different size of photos, the ImageView seems to be inconsistent different times. Like:
The teal color background the ImageView is set to distinguish the difference between the ImageView and the original Background.
This is okay. But when I choose a large size photo it seems to be inconsistent as the ImageView is overlapping the Toolbar below and this time the Camera button is no more clickable. Here is the image:
And the design gets more broken when I choose another photo. The view is showing up differently in different times. Screenshots are: and
Now what to do? I haven't done any of these design from the code side, all from the Interface Builder.
Any help will be appreciable.
I have these options in the Drawing section of Attributes Inspector:
And this is my Stack View's attributes:
Well it seems everything is fine. Just add height constraint in your Toolbar. The problem will be fixed.
First, make sure you've checked Clip To Bounds property of UIImageView in Attributes Inspector.
Second, set your constraints and / or Stack View's Distribution property in such way that all your views fits within available height.
Here's a sample: https://www.dropbox.com/s/5a7r0oz1v8vclgz/AspectFitImageView.zip?dl=0
By searching a lot of ways around, I finally was able to sort out the issue. Setting the content hugging priority(don't want to grow) and content compression resistance priority(don't want to shrink) helps a lot. Basically I needed to adjust the elements vertically. So tuning the vertical priorities for each element solved my problem. As I have to keep the size of the ImageView to be changed from time to time I've made it's vertical hugging and compression to lower than the other elements of the Stack View. Other priorities are kept as they were by default.
To visualise the situation, here is attached images. Color combinations are used here to distinguish among the elements of the Stack View.
Hope this helps.

Is there a way to fill a collection cell with two labels (fixed-scale-width) in xib?

I'm a newer to iOS, and start to use auto layout in xib.
These days I ran into an issue: Is there a way to fill a collection cell with two labels (fixed-scale-width, say, the scale is 3:1) in xib? If xib cannot do this, then how to do to achieve this?
And, I haven't turn to Swift yet, so the solution must not only for Swift.
Hope your answer, many thanks!
PS: Aspect Ratio can be the solution? But is it just a ratio for one label's width and height? as follows:
But, what I wanted is just let the ratio of widths of item1 and item2 be 3:1, no matter with the height!
Another problem is: if the cell height changed, but the width not, the ratio can keep to be 3:1?
Yes.
Create a xib
Add a collection view cell
Add two labels to it
Control-drag from the label to itself. Set the aspect ratio to 3:1
Do the same for the other label
Add the other necessary constraints to each label.
I had no problems doing this? Did you come across a specific issue?

Hide a view and adjacent gap with autolayout

I have the following configuration of a view:
Sometimes I have an image to put in the image view, sometimes I do not. If there is no image to show, I would like the Title label to be extended to the right, occupying the whole space of the image and the gap between the label and the image. I assigned a constraint "less than or equal" to the width of the image view, so I guess it might get zero width if the image property of the image view is null. But how do I remove the gap between the image view and the title label? I know it is possible from code by defining an outlet for the gap constraint, but is it possible in a simpler way, maybe from interface builder?
Nope, this isn’t possible in XIB. I’ve filed enhancement requests (starting even before this shipped). File more! The more votes this gets, the more likely it is to happen.
We ended up adding an outlet onto our subclass of ImageView that we point at the constraint we want to go away, and in the subclass disabling the constraint when the image is nil (we use different tricks to disable it in different places, since there’s no one single easy way).

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