Image is getting stretched as per UITableView width - ios

Below is how I have structure.
UITableView (304px)
- UIImageView (as seperator line) (I want this to be 288px) but it stretched to 304 px.
- UITableViewCell (304 px)
- UIImageView (background for cell) (288px)
- Label 1 for text display
- Label 2 for text display
I am stretching UITableView just to take scrollbar at the end of yellow part as shown in image.
Everything works perfect except the separator line.
Even if I put width as 288 and click on somewhere else and come back to see width of UIImageView, it get changed to 304 (which is UITableView width) automatically.
Any idea why this is happening and how to make separator line of 288 px only.
Note
The separator line is above UITableViewCell. Not inside UITableViewCell
The separator line that I am talking about is the first line in above image.
Edit 1
I also tried with below code, but still its stretched to 304px.
UIImageView *mySep = (UIImageView*)[mainTableView viewWithTag:56565656];
[mySep setFrame:CGRectMake(0,0,288,2)];
Edit 2
I have uploaded sample project at dropbox.
Please remove the line [mySep release]; from - (void)scrollingFinish {. Else app gets crashed...

I solved your problem. In table view you can add only 2 sub views like footer and header. one is above cell another is below. these views width are equal to UITableview width. So when you do something like that. Not just add the UIImageview first add UIView and set its frame then add your UIImage view here is the fixed sample project given by -Fahim Parkar
Fixed Project

Can you check your cell size?
Do you create your cell object with code or using Storyboard?
Try to resize your imageview in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UIImageView *seperator = (UIImageView*)[cell viewWithTag:_sepTag];
[seperator setFrame:CGRectMake(8,seperator.frame.origin.y,288,seperator.frame.size.height)];

This is how I fixed.
Nice cheat I can say. I added UIView and put separator line inside UIView. Set UIView background color to clearColor and let UIView stretch as much as he want.
UITableView (304px)
- UIView (have width as 304px OR 288px, but background color will be clearColor. This is very important).
- UIImageView (as seperator line) (This will be 288px).
- UITableViewCell (304 px)
- UIImageView (background for cell) (288px)
- Label 1 for text display
- Label 2 for text display
So what will happen is UIView will get stretch, but image will be of 288px only. As we have UIView background color as clearColor, we cannot see the stretched view.

I know its very old post and i was having same problem too but i rectified it by fixing the height of image view in auto layout.
hope that helps.

Related

UICollectionView cell's content's height (based on attributed string)

I've got a UICollectionView which holds 4 different types of cells, one of which is supposed to contain a UIView with a UITextView inside. The UITextView will have different attributed strings of dynamic height set in it so the entire cell also needs to resize its height accordingly - I can't have any scrolling in it.
I get the height of the attributed string through its size property. I specify the cell height as that amount in the method.
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
}
That's working quite fine - I can see that the cell is longer.
Now, for some reason I can't make its subviews longer - setting a new frame simply doesn't have any effect:
CGRect frameCellOriginal = self.textTest.frame;
CGRect frameCellNew = CGRectMake(frameCellOriginal.origin.x, frameCellOriginal.origin.y, frameCellOriginal.size.width, frameCellOriginal.size.height / 2 + atString.size.height);
self.textTest.frame = frameCellNew;
I tried using the setNeedsDisplay method on the UITextView but that didn't change anything. I also tried using setAutoresizesSubviews:YES and setAutoresizingMask:UIViewAutoresizingFlexibleHeight on both the cell and its subviews but it didn't work either.
Anyone got any ideas?
Edit:
I just noticed that the 1st time I scroll through the collection, the view containing the text is as small as in the .xib file. Then, if I scroll it again, its white background UIView becomes longer and fills the entire cell - taking the height I set it to. On the 3rd scroll the UITextView also takes said height.
I've no idea why that happens if I set all 3 views in the same method...

UIImageViews inside UITableViewCell don't become selected

I am creating the table with the custom UITableViewCells represented on this image:
In the left part of the cell there are a gray UIImageView, three labels and an UIImageView with a line. Cells can have different height, based on their content. If i insert one image as a background, this image will be stretched. So i have created two UIImageViews: top (from the top of the cell to the bottom of the first semicircle) and bottom (from the bottom of the last semicircle to the bottom of the cell) and the UIView with a pattern image background color. Pattern image in this view is repeated in order to fill UIView completely. I also calculate height of the cell in order to UIView join bottom UIImageView correctly.
My problem is: when the cell is selected my imageViews are not selected. Only middle UIView becomes selected. You can see this in the pictures below:
I want all imageViews(top, bottom ad gray) to become selected with the cell. How can i do this?
You have to override the method in your UITableViewCell subclass.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
self.imageView.image = [UImage imageNamed:#"some_other_image.png"];
}

center custom UITableViewCell

I have a custom UITableViewCell subclass with width of 300.
Since the cell is 20pt shorter than the table view, When the cells get loaded, its placed on the left most position, is there a way to center this custom cell to the tableview's center?
I tried cell.center = self.tableView.center in cellForRowAtIndexPath, but it wont work. any idea?
You can pad your custom cell with 10px to the left so it will only appear to be centered, so you just make the width 310 and move everything else inside by 10 px.
a) Resize your UITableView to 300 and center it.
or
b) Make your cell's width same as UITableView's width. Make your custom cell's background color clearColor. Add one more view to your custom cell subclass that will contain every other view and add every view in that view. Think of this like an illusion where the cell is actually wider but it's contents are centered in a fake view.

Adding space between UITableView cells and making them round cornered

I have a UItableView in which I am loading RSS. I would like to leave a space between each cell in the table view, as well as giving round corners to each cell, to give an effect like this one:
The problem is that I can't seem to find working code. Using:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10.; // you can have your own choice, of course
}
only adds space on top of the UITableView...
The easiest way to create this UI is to create a custom UITableViewCell that will have that extra space at bottom, if you can create a .png file that will be the gray background with the rounded corners or you can round the corners with view.layer.cornerRadius (don't forget to include QuartzCore for cornerRadius), add a label and an arrow and that's it.
No need to customize anything.
You have to initialize your tableView with a "groupedStyle". Or, if you you're using storyboard, set the style there. Then make the tableView background color to whatever you want.
myTableViewContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
the heightForHeaderInSection thing is for sections, not rows.
you can do heightForRowAtIndexPath if you really need to for some reason.
You can first make cells to be clear color background, then add a rounded corner view which should be smaller than the cell. This makes it looks like it gets a spacing around the cell.
This is an older one, but I ran into this problem recently. If your cells have a custom uiTableViewCell class add this to the bottom of the .m file of the class (just above #end)
- (void)setFrame:(CGRect)frame {
frame.origin.y += 4;
frame.size.height -= 2 * 5; //adds the space and gives a 5 point buffer, can also indent the width using frame.size.width
[super setFrame:frame];
}

How to prevent UITableViewCell backgroundView from stretching

I'm attempting to add a background image to a UITableViewCell so that each cell can have some spacing at the bottom.
I'm setting the cell's backgroundView property like this:
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell"]]];
The "cell" image is 20px taller than the actual heigh of the UITableViewCell, this is on purpose so that my cells can have some spacing at the bottom. Only problem is, the "cell" image will get stretched all the way down the cell's contentView.
Is there a way for me to prevent the cell from stretching the backgroundView all the way down?
Thanks in advance!
Look into the contentMode property of UIView. If the height of your background image is smaller than the height of the cell and you want the image to "stick to the top" try setting
cell.backgroundView.contentMode = UIViewContentModeTop;
Look at the "Content Modes" section on this page for more information. It mentions...
"By default, the contentMode property for most views is set to UIViewContentModeScaleToFill, which causes the view’s contents to be scaled to fit the new frame size."
Two options:
1) Make your image bigger manually, filling it with transparent space in the gaps. (Easier solution).
2) Add another UIView inside your UITableViewCell, make it as big as the image. Add your text label (if any) inside this view.

Resources