I have a table and a cell with a text and image. i want to remove the imageview completely from the cell because the cells are dynamically generated and some have images and some dont.
i have tried:
imageview.hidden = true
and i also tried:
imageview.removeFromSuperview()
but those remove the image but not the image view. right now, when there is no image, the image does not show up in the imageview but there is a huge gap below the text with no image because it seems like the imageview is still there. i want to remove the imageview completely so the huge gap is not there for cells with no images.
It's important that cells were reused. Don't remove because you didn't add it back. And if you hide image view, you should show it when this was needed. Like this:
if needed {
imageview.hidden = false
} else {
imageview.hidden = true
}
Using Autolayout you can solve this problem. Dont give fixed height and fixed width. And check content hugging/compression priority of imageview is properly set or not.
Related
I am trying to build a UITableView which cells are containing a UIImageView and a few UILabels.
Not in general but sometimes updating the list via reloadData().
My table images conflict with UIViewContentMode.ScaleToFill and clipping to bounds.
The cell will appear like this, extending it's edges.:
These options are set for the image in awakeFromNib:
self.miniImg.layer.cornerRadius = self.miniImg.frame.width/2 // Circular
self.miniImg.clipsToBounds = true
self.miniImg.layer.masksToBounds = true
self.miniImg.contentMode = UIViewContentMode.ScaleToFill
My idea was to try and fix this by clipsToBounds and masksToBounds. But it didn't work.
So, I have the following setup in storyboard:
storyboard image showing the setup
The code is setup so that that image is an optional. As it stands, when the object passed to this view controller doesn't have an image in it, the image becomes hidden. The problem is the gap that remains between paragraph1 and paragraph2. I want to have it so that if the image isn't in the object being passed to this viewController, then paragraph1 and paragraph2 come close to each other.
If you need any more info about the setup, I'm all ears(well, eyes, but hey...) Any direction would help. Thanks.
In your cell subclass, add an outlet to the UIImageView height constraint. When you configure the cell, e.g. in tableView:cellForRowAtIndexPath:, check if the image is nil, like this:
if let _ = cell.myImageView.image {
cell.imageViewHeightConstraint.constant = xx // put your image height here
} else {
cell.imageViewHeightConstraint.constant = 0 // give the imageView a height of 0
}
In the interface builder, I'm trying to create a prototype cell with an image that covers the entire cell but it is not running how it is expected.
As you can see in the following screenshot of my interface builder, I have an image view covering the entire cell, and is constrained to each edge of the cell:
And in fact this is how I expect it to look on the simulator, but instead I get this:
Where as you can see, it is not anchored all the way to the sides, and it may be hard to see, but the image actually extends past the bottom of the cell (if you look hard enough you can see the separator striking through the bottom portion of the image.
This is really buggy and I really have no idea what's happening.
Perhaps adding aUIImageView inside of your cell in code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//configure cell
let imageView = UIImageView(frame: self.cell.frame)
imageView.image = YOUR_IMAGE
imageView.center = cell.center
imageView.frame.size = cell.frame.size
imageView.contentMode = .ScaleAspectFill
cell.addSubview(imageView)
}
Hope this helps.
I think you accidentally disabled cell's Clip Subviews in code or in Storyboard, by default It should be enabled.
If it's not the cell, check it's Content View.
By the way, by disabling Clip Subviews for both Cell and it's Content view, I managed to reproduce your bug.
Seems that your image constraints are relative to cells contentView margins. You can disable it, see screenshot. Be sure that constant is 0
You need to do Clip Subviews (clipsToBounds) on cells contentView or imageView if you don't want aspect filled image to go beyond bounds. Otherwise you should use Aspect Fit, or Scale To Fill, or do the math manually
This is because you are setting constraint to margins.
When adding constraints to uiimageview. Uncheck constraint to margin.
I will have about 12+ (any multiple of 4 per row) images that I want to have inside a ScrollView. See example below, the 12 circles (with the last row cut off a bit) are the UIImageViews.
I am not able to add the UIImageViews inside the ScrollView. The width,height of the scrollView is 250,250
This is what I have tried:
func setScrollView() {
scrollView_avatars.contentSize = CGSizeMake(250,300);
let imgView1: UIImageView = UIImageView(image: UIImage(named: "img1.jpg"))
let imgView2: UIImageView = UIImageView(image: UIImage(named: "img2.jpg"))
let imgView3: UIImageView = UIImageView(image: UIImage(named: "img3.jpg"))
scrollView_avatars.addSubview(imgView1)
scrollView_avatars.addSubview(imgView2)
scrollView_avatars.addSubview(imgView3)
}
iOS noob here
My code above gives me very bizarre output, i think the sizes of the UIImageViews are not set correctly.
I dont also know how to position the UIImageViews within the ScrollView like only part of a image is visible.
As #rmaddy says, you need to set the frame properties on your views. You will need to do math to calculate their positions based on row/column position in your grid of views.
If you're using AutoLayout you may need to generate constraints to set the view's sizes and positions. (I haven't tried to add views to a scroll view in code with AutoLayout before, so I'm not totally positive what happens if you just set their frames without defining constraints.)
You need to declare your image views like this:
if let image = UIImage(named: "myImage"){
let imageView = UIImageView(frame: CGRectMake(yourXpoint, yourYpoint, image.size.width, image.size.height))
imageView.image = image
}
where yourXpoint and yourYpoint is where you want the image origin to be set, this way you are saying to swift where do you want the images to be place and what is its size.
The if let is just to be safe in case the image does not exist.
Another good option is to set constrains as it will make your app have a nice layout in al different screen sizes and orientation
I've been looking around and can't find a solution to my problem. I have a UIImageView as subview of UITableViewCell, I add it in cellForRowAtIndexPath, the frame of subview starts on cell-1 and goes out of cell-1, getting into cell-2.
Basically, I want my image to be on the middle of two cells. Now, when tableViewfirst loads, it appears correctly, but when I scroll the cells down and go back, the image is trimmed, showing only the part that is in cell-1 bounds.
I know iOS 7 has a scrollView between contentView and cell. When I log on my cellForRowAtIndexPath:
if (cell.contentView.superview.clipsToBounds == YES || cell.contentView.clipsToBounds == YES || cell.clipsToBounds == YES) {
NSLog(#"CLIP = YES");
} else {
NSLog(#"CLIP = NO");
}
it shows me that cell, contentView and scrollView have the property clipToBounds set to NO, which is what I want.
I just don't know why subviews still get trimmed.
I ran across the same problem as you.
I realised that the problem was my cell had non-transparent backgrounds. The subviews weren't being clipped, they were simply getting covered by the background colours of previously added cells.
You can't control the order in which cells get added to the table view, so you'd have to use [UIColor clearColor] as the background colour of your cells to get the desired effect.
If that's an issue, set the zPosition of your subview to a high enough number and you'd hopefully be good to go.