UIImageView Ignores AutoLayout Constraints in UITableViewCell - ios

I'm having a problem with a UIImageView inside of a UITableViewCell when using autolayout. Specifically I'd like to have the image separated from the edges (20pt) , with a strict size(50pt) and so I have defined this:
imageview to top = leading to edgeview = 20
height = width = 50
What i find is that the imageview completely ignores its constraints(sorry about the scary smiley, it's what google images gave me). Naturally my desired layout would have more content, but regardless of whether that content is present the effect is the same.
The setup for the project is as follows:
Make a single view app
Add a table view, connect the data source and write boilerplate to make at least one cell
create a tableviewcell with nib,
add an imageview to the tableviewcell constraints to top/leading (10 works, with a height of 20)
set an image in the imageview so you can see it at runtime
I feel like that should be sufficient, but it seems not to be working, am I missing something silly? For comparison this same setup causes no problem without a tableview, the imageview behaves just the way expected

It turns out I had missed one step that kept Anna's version working, I had connected the UIImageView with a property named imageView (overwriting the UITableViewCell default property), apparently the constraints in the superclass were stronger than whatever I was adding.

I couldn't quite get this to work myself, but I think you'll get it if you try some of the things in here:
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

I think that it doesn't ignore it.Your setup is correct.
You just have to stop drawing outside image view.
Check this on ImageView: Clip Subviews ->enabled. Choose also good View fill mode: Scale To Fill or Aspect Fit.

Related

UIImageView: setting the margin breaks the image frame

I'm relatively new to auto layout design, and this problem is bugging me so bad. I have a UIImageView nested inside a Collection View Cell. The margin of Collection view is set appropriately. When the margins of UIImageView are not set, the image is displayed fine, but when I set the margins, the frame suddenly gets larger. Why is this happening? I added a link to the video of my problem.
https://vimeo.com/501830557
I would need to look at your code to give you an answer related to what is wrong with your code right now, it could be that the constraint is related to the wrong parent or it could be something else (related to the very collection view).
But I can give you an way to configure completely your CollectionViewCell through .xib: https://medium.com/#michaelrojas_66889/how-to-make-a-custom-collection-view-cell-in-swift-6d5783ab7c1c

ios - UITableViewCell does not fill the entire width of the table

I think the cells don't fill the parent width because of indicated dimensions 300x300, but I don't know how to change it to parent size.
TableView has constraints on the boundaries of the device. I also tried set constraints for the cell but it didn't help.
You should set constraints in your image and on entire UITableViewCell xib just like in ViewController
This is a very basic design task. You really should take the time to go through a few tutorials on Auto-Layout.
The comment you posted: "Adding constraints in the UITableViewCell xib is not possible for me," along with the image, indicates you are selecting the Content View in your cell xib, but that is not where you want to add constraints.
You need to constrain the elements in your cell to the content view:
which results in a table view (at run-time) that looks like this:

Custom Collection view cell in swift

I am new to iOS development and I am developing a buy/sell application. I am trying to make a collection view where I don't hard set the size of the collection view cell. Instead it would depend on the user who post up something they are selling. I realized that by hard coding the size of the boxes, everything looks "boxy". Any suggestions would be greatly appreciated.
Assuming you're using the default flow layout, there are two options:
You can have self-sizing cells if you:
define unambiguous constraints between your cell and its subviews;
do not define fixed width/height constraints for the image view;
set the estimatedItemSize of your UICollectionViewFlowLayout:
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = CGSize(width: 100, height: 100)
Auto layout will then resize the image view to fit the image, and the cell will then resize itself to fit that, accordingly. So, for example, with some random image sizes, the standard flow layout will resize the cells automatically for us:
Now this is a simple cell with a randomly-sized, solid color image in an image view and constraints of H:|[imageView]| and V:|[imageView]|, but the same idea works with complicated cells, too. You just need to make sure that the top/bottom and leading/trailing constraints of the cells are unambiguous.
Note, if you rely upon the implicit size of the image view, this assumes of course that the images are already sized consistently for the device. If not, you might want to create constraints for the image view height/width, include #IBOutlet references for that, and then set the constant for those constraints in your cellForRowAtIndexPath method. But, to manage memory efficiently, you generally want to downsize the images to an appropriate size anyway, so this is generally not necessary.
Alternatively, you can specify a delegate (a UICollectionViewDelegateFlowLayout) of the flow layout and then implement sizeForItemAt:.

UIImageView overflowing custom tableview cell

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

UITableView resize cell based on content

Hi I need help with understanding how to resize cell based on its content.
So first of all of course I found many links:
iOS: Multi-line UILabel in Auto Layout
https://www.youtube.com/watch?v=CkvZEJ7dIfw
https://github.com/williamhqs/GSTableViewDynamicHeight
As we said about git example (last link) I can't understand how to change some label and make it works. For example if I delete UILabel and create new one and bind it with content property (content - it is IBOutlet property of bottom label). I seems lost some setting and cell won't stretch.
So I think I don't know to much understanding how to do it.
What I want to understand:
How to setup auto layouts in the storyboard or programmatically.
Which thing I should handle programmatically to make it done.
How the preferred size affects on label. Do we need every time use bonds of superview as a preferred size?
Also in the git example we have one label that changes itself size. What if we have 2 UILabels with dynamic content how to setup it?
If you have some links or videos please drop them here, because I really stuck. Thank you!
I know it's duplicated question but I can't understand how to setup it.
To make Self Sizing Cells works there is three steps:
Setup correctly the constraints of the cell (most important the top and bottom constraints)
Specify the estimatedRowHeight of your table view
Set the rowHeight of your table view to UITableViewAutomaticDimension
for step 2 and 3 it's so easy just add these two lines in you viewDidLoad method
tableView.estimatedRowHeight = 44.0 // or whatever you want
tableView.rowHeight = UITableViewAutomaticDimension
for step 1 which is the most important part of self sizing process
what you need to keep in mind is that "self-sizing needs to get its size from your constraints."
Basically we need to make sure that top space and bottom space constraints of our cell is correctly set up here is an example :
just two labels with the constraints top,bottom,right and left space to each other and to container (see image) the UILabels Lines property is 0 so that you allow have multiple lines
Another example from a project that I am working on:
bottom line:
The label constraints should push against the size of the table view cell and make it taller. So always check the top and bottom constraints.
Also you may run intro trouble working with UILabel with multi-lines because
The intrinsic content size of UILabel and NSTextField is ambiguous for
multi-line text.
so make sure always to setup the preferredMaxLayoutWidth property of yout label:
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}
for more infrmation about this issue here read :
iOS Autolayout multi line UILabel
iOS multi-line UILabel in Auto-layout
preferredMaxLayoutWidth
And for more information about Dynamic Table View Cell Height in general here is some useful resources:
UITableView Tutorial: Dynamic Table View Cell Height
Understanding Self Sizing Cells and Dynamic Type in iOS 8
Self Sizing Table View Cells
Mysteries of Auto Layout, Part 1 (wwdc 2015)
Not sure if it will apply to AutoLayout, although when laying out a cell programmatically, running "layoutIfNeeded()" fixed my cell content stretch issue.
layoutIfNeeded was ran at the end of a configure method, used to map values (view model) to UI objects (views)

Resources