I am using the attached prototype cell layout in a tableview. Each cell is sized using autolayout:
tableView.rowHeight = UITableViewAutomaticDimension
The "tagline" is a string and sets the height of the cell depending on the length of the string. Given a greater length the height of the UIImageView also resizes. The ImageView is set to a variety to images with different sizes and aspect ratios. I would like to resize the image based on the beginning width of the ImageView(that does not change) and the resulting height and the image's aspect ratio.
The code to set the two is very straightforward.
cell.taglineLabel.text = tagline
cell.recipeImageView.image = image
Inside a tableView cellForRowAt function I set the tagline first in the code, then the Image. However, if I attempt to resize the image and I inspect the bounds or frame size, the size is the original size. I am assuming the autosize is performing after the func. Therefore, I cannot determine size.
Is there a way to force the autosize after the tagline is set, so I can see the frame/bounds sizes I have to work with?
Related
I'm using UIImageView with Content Mode set to Aspect Fit. UIImageView resizes correctly my image but it doesn't change own frame after resizing even there is no constraints set.
What I need:
I need UIImageView to resize own frame according image inside. I've configured image view at Storyboard.
I need to layout again other objects at view because UIImageView's size is changed.
What I've tried:
I've created:
UIViewController (white background) → UIImageView (gray background) → sample image inside it.
Constraints: leading & trailing constraints set to zero, Y constraint is set to align UIImageView vertically.
As you see after image scaled correctly UIImageView's frame is still has incorrect size (gray background).
I've tried to set UIImageView size manually:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newSize = AVMakeRect(aspectRatio: backgroundImageView.image!.size, insideRect: backgroundImageView.frame)
backgroundImageView.frame = newSize
}
In that example code I've calculated image size and set frame size to image's size. I don't know if it is better way so I don't need to use AVFoundation's methods.
Problem: other object is place incorrectly because UIImageView's size changed and constraints are invalid now.
That methods doesn't work for me:
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
Even I will call my UIImageView size calculation at viewDidLayoutSubviews() other constraints is still using old UIImageView's size.
Questions
How to change UIImageView's size correctly? Maybe I can do it without code? Maybe I need to play with Hugging/Compression? I've tried something but got no luck.
How to force view to recalculate other constraints so they will use new UIImageView size? For example I can put red square at the top of UIImageView, but after UIImageView has been resized by my method, red square still will be at the old UIImageView's position.
I have a UIImage in a tableview cell that I want to show in a circle. When my table is rendered the first time the images are shown in a diamond shape instead of a circle, but when I go to that same screen after the first time the images are rendered in circle. I have found the problem, but I don't know the solution. Here's the problem: In the storyboard the UIImage view has a width and height set to 135, this is a random number because I assumed that I could overwrite it with the following two constraints in the storyboard: 1) a constraint that sets the height proportional to the superview's height and 2) a 1:1 ratio for the height and the width of the UIImage. Here's the snapshot of my constraints:
I added prints to tableView:cellForRowAtIndexPath where I change the layout to a circle to see the UIImage width. The first time it prints 135 and the second time it prints 100. This means that the proportional size constraints weren't effective in the first round, but they are effective after. I tried to set the fixed height and width in the storyboard to zero but that didn't help, it showed the image in a square. How can I enforce the proportional constraints all the time? Or disable the fixed size properties for this particular view? Thanks!
P.S. Here's the code inside tableView:cellForRowAtIndexPath that changes the layout to a circle (nothing else in that method that touches the image or the layout):
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2
cell.imageView.clipsToBounds = true
cell.imageView.image = data.image
Try to reload table in viewWillLayoutSubViews.
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self.tableView reloadData];
}
I have a problem. I'm trying to implement dynamic cell height in the annex, when I have nothing. Normally, in my cell I have one UIImageview and three UILabel. My task is to implement dynamic height of the text without changing the picture size. The picture comes from the server and scaled inside the cell with the mod Scale to fill. My Text does not change its height in the cell.
self.tableView.estimatedRowHeight = 415.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
I always found numberOfLines = 0;
The problem is that the picture is stretched to the server, which should not be a text of its size does not change.
The problem is that you have not placed any limiting constraints on the image view's size. Therefore it adopts the size of the image.
I have a UIImageView in custom cell ,Maximum size of UIImagView Can be 100X100. if there is no Image in UIImageView than its height should be Zero. How to specify constraint for this in xib?
Observer left cell image. "There is blank space of height 100 because cell does not have any image to display." There should not be any blank space between hello and heart button.
Right cell image is fine.There is image to display so cell is displaying it.and other component are coming below it.
Put the image inside a div element and set the max-height css property to 100px.
If an Iboutet is connected to the height constraint of the UIImageView then in the custom cell class you may check if there is image set the height constraint a value otherwise set to 0 and the heart and comment components should be pinned to image view.
So that on setting height constraint to 0, rest of the components move closer to "hello".
First of all I'm trying to lazy load images from remote url and what I've done so far is as follows,
- I have created custom UITableViewCell which contains on UIImageView and other labels using XIB and mapped it to my custom UITableViewCell class.
- I'm using the custom table cell in my UITableView
- UIImageView inside the custom cell, loads remote image as per Apple's tutorial for lazy loading images for tableview
- Initially ImageView displays an loading image, once the image is loaded, the loaded image gets updated inside ImageView or if it fails, No Image will get displayed
And the problem here is, the remote images are of variable sizes, lets say if the image is smaller than the ImageView frame the image stretches and display as blurry but once we scroll or on row selection its re-size to actual size and looks fine.
At the same time if the image is larger than the ImageView frame it fits correctly fit inside the frame but again if we scroll or select the row it occupy size bigger than frame and hides other elements in the row
And I searched for hours and all I got is setting content mode to UIViewContentModeScaleToFit and setting clipsToBounds to 'YES', I made those changes as well both in XIB, as well as in Code but it doesn't change anything. Can someone point me, on what I'm doing wrong or how to fix this issue.
Thanks.
Grab the image however you want
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];
Then check the size of the image using:
NSLog(#"the customImage width is %f and height is %f ",customImage.size.width,customImage.size.height);
Then modify the width and height of the frame that your placing it in so that they are set to an equal fraction of whatever the original image was (while still fitting in the necessary bounds)
float newWidth = customImage.size.width *.4;
float newHeight = customImage.size.height *.4;
ImageButton.frame = CGRectMake(x coord, y coord, newWidth, newHeight);
You can also throw on aspect fit/fill if necessary
I just re-sized the image, if the image's width and height is greater than the frame size and displayed the re-sized image on it, and if the image dimension is smaller than the frame I displayed as it is.