I have a UITableViewCell that only contains a UIImageView. I am using UITableViewAutomaticDimension which is working perfectly for all the other cells in my tableview, except for this image view cell.
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit. I set the UIImageView's background to be red. And the cell above it is white. The actual image is black down below.
When I do that, the cell has about 100 px above the image and 100px below. but the images size is exactly what I want. Has anyone had the same issue?
Because that red space isn't always the same size and it was a lot taller when I have a really big image in there, I believe the layout thinks the height should be the actual height of the image, without any aspect fit constraint.
Thanks for any help!
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit.
You can't do all that with only auto layout. This is because a UIImageView's intrinsic content size is just its image's full size.
Instead, calculate the appropriate height and set it when you set your image:
let imageViewWidth : CGFloat = // the width you want…
imageHeightConstraint.constant = image.size.height / image.size.width * imageViewWidth;
Note that this will fail if you have a 0x0 image due to division by zero. If that's possible in your app, check for this case and just set the height constant to 0.
At this point, reload the row so that the table view will recalculate the height.
Related
I am trying to set a UIImageView has no fixed size and width in the storyboard. I set the leading space to container to greater than or equal to 20, top space to the container to greater than or equal to 20 and trailing space to container to greater than or equal to 20 and vertical spacing to a UICollectionView, the UICollectionView satisfy the constraints. But the UIImageView don't work, it's telling me it's Position and size are ambiguous. How can I make it work? My Idea is no matter how large the image view, it will always keep 20 space to leading, top, trailing and bottom.
I have set the height and width to be greater than or equal to some value and their priority to 999, it's lower than the leading, top and trailing priority. and I have set the imageview to center horizontally. but i get height and vertical position are ambiguous for “Image view”.
Can you see what happens when you add height and width constraints to the UIImageView, but set their priorities to some low value, e.g. 1? It should help, but now UIImageView won't be centered. You can fix it by enclosing UIImageView in another view (empty UIView) and centering it in it both horizontally and vertically.
When you get this error:
Position and size are ambiguous for “view”
Probably following constraints are missing/incorrectly set:
Width constraint (size issue)
Height constraint (size issue)
Alignment constraints (position issue)
If you are using Stack Views, make sure the Photo Image View is inside the Stack View! (As in the Apple Tutorial)
If you want to give margin kind of thing into button through coding than please try these if it'll work
[button_Name setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 12.0, 0.0, 0.0)];
I hope this will work..!!!
In cases where the height/width are not ambiguous at runtime, but ambiguous at storyboard time, e.g., setting an image dynamically, having a container view, etc, I try to add constraints with the placeholder checkmark checked to silence xcode.
I want to resize or scale a image down by it's width is fixed. I have been searching all over the network, it's about resizing the image to new size by specifying new CGSize value. I only want to scale it down by its width is fixed, like 100px, and the height scale down and Maintain it's Aspect Ratio.
check this out and modify as per your needs.
http://code.developwithus.com/iphone/resize-image-in-iphone-sdk/
Setup: I have a scrollview with a label, uiimage, and a tableview (grouped) nested within. The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The tableview should extend in height depending on the length of the content received from the web service. Then set the scrollview height to accommodate the height of the tableview
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in many scenarios.
The width and height of the cell are ignored; the cell cell is re-sized according to the value you return from -tableView:heightForRowAtIndexPath: or (failing that) tableView.rowHeight. It might appear as if the cell is big enough if the label in the cell is sized to be big enough, because the label is allowed to be bigger than (and render outside) the cell.
One way is to override -tableView:heightForRowAtIndexPath: to return the correct height. This isn't really the intended use of UITableView (it's primarily designed for lots of rows of a single height, dynamically generated from a list of content).
Another way is to set tableView.tableFooterView = myCustomFooter instead. This is probably the easiest way. Size it correctly before performing the assignment (the height matters; the table view will set the width for you anyway). Also make sure that the autoresizing flags are not set, or the size will appear to randomly change when the table view changes size (e.g. on autorotation).
I'd just focus on the variable height element, figure out the height that fits the text there. To figure out the height that a string will take when rendered use a snippet like this:
CGFloat MY_TABLECELL_WIDTH = 320;
CGFloat MY_MAX_HEIGHT = 10000;
UIFont *MY_FONT = nil; // load the correct font here.
CGSize maxSize = CGSizeMake(MY_TABLECELL_WIDTH,MY_MAX_HEIGHT);
CGSize textSize = [serverString sizeWithFont:MY_FONT
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
I have setup a UITableView (320 wide) with a UITableViewCell created in IB, the cell is also 320 wide. After setting a background image (320 wide) in UITableViewCell (or my subclass to be correct) I have noticed that the cells fall short of the right hand side of the UI (notice the blue on the selected cell and the grey on the one above) Does anyone know what is causing this?
Cheers Gary
I was setting the seperator style and the seperator color on the UITableView, this takes 1 pixel off the cell height for the seperator and offsets the backgound by about 5 pixels to the right. If I reduce the background image thats going into the cell from 320x65 to 320x64 it fits perfectly with no offset.
The only difference between the two screens below is:
LEFT: UITableView Seperator = "None" (UITableViewCell background has no offset)
RIGHT: UITableView Seperator = "Single Line" (UITableViewCell background has offset)
DATA:
Cell resolution: 320x65
Cell view: 320x65
Cell Background Image Resolution: 320x65
If you want use the separator your Cell Background Image needs to be 320x64, this will stop it shifting sideways and display the cell correctly.
Cheers Gary
It seems to me that you're just not setting the contentMode of your background image view correctly (i.e. you're setting it to aspectFit or something). Are you sure that it's offset by 5, and not actually shrunk by 5 pixels?
For example, if the original picture is 320x65, and the height is reduced to 64, aspectFit will scale it, while keeping the same aspect ratio, to 64/65 * 320 ~= 315.
I use a full screen imageView to display the image, as follows:
UIImageView*imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]
imageView.contentMode=UIViewContentModeScaleAspectFit;
imageView.image=srcImage;
As the srcImage size differs, its position (frame) in the imageView differs, how can I
get the position of the image in imageView?
thanks a lot.
You have to do the math yourself. Calculate the aspect ratio of your image and compare with the aspect ratio of the image view's bounds.