how set image set full width and set UIViewContentModeScaleAspectFit? - ios

I want to set a image for UITableviewcell but imageview contentmode is UIViewContentModeScaleAspectFit and also set full width depends on width height of UIIMageView is change,how to do that.

Change your UIIMageView content mode to UIViewContentModeScaleToFill or UIViewContentModeScaleAspectFill
Edited
Use following UITableView's Method
Add you UIImageView Height in NSMutableArray (heightsArray)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [heightsArray objectAtIndex:indexpath.row];
}

Related

Label width in TableView not shrink according to sizeToFit autolayout

I am trying to make a chatview with table but my label width not responded to its width.
constraints for label
constraints for bubbleview
code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//...... some code here
oLblMessage.text=chat.iText;
[oLblMessage sizeToFit]; // this doesn't responded at sll
NSLog(#"%f",oLblMessage.frame.size.width);
[oLblMessage layoutIfNeeded];
[cell setNeedsUpdateConstraints];
return cell;
}
result
Even I tried to set priorities but not succeeded.
SO any solution for this....
sizeToFit will not set it width according to text size becauase of current constraints. Add width constraint in label, and make it less than or equal to. in cellForRowAtIndexpath calculate size of text and assign size.width to lableWidth contstraint constant through its iboutlet.
set your constraints as below
for Bubble:
For Bubble
for Label:
Maybe this will help you..

table cell height issue iOS?

I have custom table view.I have added 4 views in that
1.ImageView
2.Three labels
Now I want that image should increase in size for multiple devices.For that i have given it below constraints.Proptional height is 176:339
Now in order to increase the height of the image i have increase height of table i used below code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(!self.customCell)
{
self.customCell = [self.tableview dequeueReusableCellWithIdentifier:#"tableCell"];
}
CGFloat height;
height=self.customCell.img_main.frame.size.height;
height=height+self.customCell.label_one.frame.size.height;
height=height+self.customCell.label_two.frame.size.height;
height=height+self.customCell.label_three.frame.size.height;
height=height+self.customCell.dev_name.frame.size.height;
NSLog(#"height is %f",self.customCell.img_main.frame.size.height);
return height;
}
The heigh of image is always 176 which i set in interface builder for 4 inch screen.
Why the height of both images & table view cell is not increasing ?
This is for your second question, if you want to change the image view's height base on the image it display, i suggest to use Height Constraint instead of Proportional Height Constraint, the calculation will be easier. When you get the image, and know the image size, so you calculate and get the size of the image view, and then update the Height Constrains. Done.
drag the constraint to your cell
update the constraint when image setted
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
cell.image = image;
cell.imageViewHeightConstraint.constant = [self calcuteSizeWithImage:image]; //you need to do the calculation yourself
return cell;
}

custom cell height from UIImageView size?

I have a UIImageView inside a custom cell. If there is no image to display in cell then cell height should automatically decrease.(ImageView Height should be zero in that case). How to specify constraint for this in xib ?
Your UITableView data source should represent the existence of this image.
You should use the following delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.dataSource[indexPath.rox].imageExists) {
return 50.0; // Change to your value with image
}
return 10.0; // Change to your value without image
}
Your UITableViewDelegate should implement tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// add your condition here if image exit or not and set cell size dynamically
return [indexPath row] * 20;
}
You will probably want to use NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to calculate your row height rather than just performing some silly math on the indexPath.
Here is good sample code for dynamic size cell.

UITablViewCell dealing with changing the size of the cell

Iam creating a custom UITableViewCell programatically , I need the height of the cell (which I changed) , at my custom cell I get the cell's height by self.contentView.bounds.height , which gives me the original height (not the changed one ) , how can I get the changed height ???
You MUST calculate cell height in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Otherwise tableView uses Row Height from Interface Builder Size inspector for tableView.
You can use this
CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
//To get the height
NSLog(#"row height: %#", frame.size.height");

How to set UITableViewCell height depending on screen size

I download data from the web, it consists of text and image. I'm putting this data into UITableView, and I want image to be a background of UITableViewCell.
All the images has the same resolution 620x350.
All the iPhones have different screen sizes so I need to scale image's width and height depending on screen size,and also do this to UITableViewCell.
What are the best solutions?
Thank you.
The BEST and clean solution, it is just playing with the height of the cell, letting the image be resized automatically by Autolayout.
It is easy:
In your cell, add an UIImageView and set the size you want to have when the cell has the actual height;
Set as constraints of this UIImageView, at least top, bottom and one of the lateral border (right or left); set also the constraints to keep the proportion width/height;
At this point, when the cell grow, the image grow in height, and to keep the proportion (thanks constraint) it is resized the width as well, having ALWAYS the correct and proportioned size.
Now therefore, you have just to write the code for the cell's height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
static CGFloat cellDefaultHeight = /*the default height of the cell*/;
static CGFloat screenDefaultHeight = /*the default height of the screen i.e. 480 in iPhone 4*/;
CGFloat factor = cellDefaultHeight/screenDefaultHeight
return factor * [UIScreen mainScreen].bounds.size.height;
}
Obviously, set the contentMode of the UIImageView to AspectFill.
Simply use - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat aspectRatio = 350.0f/620.0f;
return aspectRatio * [UIScreen mainScreen].bounds.size.width;// assuming that the image will stretch across the width of the screen
}
You can use the -tableView:heightForRowAtIndexPath: method in HTTableViewDelegate to return the height for each cell. In your case, you will want something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UIScreen mainScreen].bounds.size.height;
}

Resources