how to set frame for cell.imageView.frame? - ios

i tried this below code in cell for row...
if([cell.textLabel.text isEqualToString:#"Home"])
{
cell.imageView.image = [UIImage imageNamed:#"Home-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
This is below code is in didselect row at Indexpath...
if ([[arrCell objectAtIndex:indexPath.row] isEqualToString:#"Home"]) {
cell.imageView.image = [UIImage imageNamed:#"Home1-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
I am using this above code to display image in tableview cell and when cell is clicked Then Image change to another image...Images are Changing But,the frames are not applying...

No you can't do this because it is fixed layout of UITableViewCell.If you want to do this go with below option
OPTION 1: CustomCell
Through CustomCell you can set image view frame whatever you want.
OPTION 2: Programmatically creating image view add it to cell
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"yourImageName.png"];
[img setFrame:CGRectMake( 10, 15, 20, 20)];
[[cell.contentView addSubview:img];];

Can be achieved by transform scalingļ¼š
cell.imageView?.image = UIImage(named: "icon_shareToFriends")
if let imageView = cell.imageView, imageView.transform == .identity {
imageView.transform = imageView.transform.scaledBy(x: 0.55, y: 0.55)
imageView.contentMode = .center
}

Related

iOS "resizableImageWithCapInsets: resizingMode:UIImageResizingModeStretch" this image how to set capInsets?

I must ensure that the arrows do not stretch. So how should I set capInsets?
Please check this image:
UIImage *originalImg = [UIImage imageNamed:#"yourimagename"];
//Either you can use this
//UIImage *resizeImg = [originalImg resizableImageWithCapInsets:UIEdgeInsetsMake(4, 50, 30, 50) resizingMode:UIImageResizingModeStretch];
//or you can use this
UIImage *resizeImg = [originalImg resizableImageWithCapInsets:UIEdgeInsetsMake(4, 214, 30, 55) resizingMode:UIImageResizingModeStretch];
//Before resize
self.original_Imageview.image = originalImg;
//after resize
self.resized_Imageview.image = resizeImg;
You can use this code to set your image
[This is output image][1]
[1]: http://i.stack.imgur.com/iJV9X.png

Set image on tableView Cell size

I have tableView Cell size and need to set image on tableView cell., but image not set of define the Cell size (following code into tableView Cell size stored cellSize variable).
My Code is...,
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
cell.imageView.image = [UIImage imageNamed:#"logo_black.png"];
above code in image not set image properly in tableView Cell.
So, How to set image on TableView Cell?
If you are trying to say that the image u are setting in each cell is not getting displayed at the right position then u can try doing the following:
first create a prototype cell and position the UIImageView at the right place in the cell u want and go to the scale properties of UIImage and note down the co-ordinates... then
instead of
CGRect cellSize = [tableView rectForRowAtIndexPath:indexPath];
cell.imageView.frame = cellSize;
this you can write the code as follows:
cell.imageView.frame = CGRectMake(x,y,width,height);
ex.
CGRectMake(0,0,20,20)
CGRect cellSize = cell.frame;
cell.imageView.frame = cellSize;
Try this code:
UIView *bgView = [[UIView allow ] init];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#"imgName"]];
cell.selectedBackgroundView = bgView;

Where to add image layer for xcode objects?

I want to add a border around my UIimageView?
Let's say everytime I add a new photo:
flagImageView.image = UIImage(named: correctAnswer)
the photo is different, and I need to re-scale.
Where do I put the code, although its not the right answer. I am unsure how to rescale the imageview
// Define a new image object
UIImage *image = [UIImage imageNamed:#"lake.png"];
// Define a new image view
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 157, 125)];
// Set image in imageview and add to view
[imgView setImage:image];
[[self view] addSubview:imgView];
// Create a white border with defined width
imgView.layer.borderColor = [UIColor whiteColor].CGColor;
imgView.layer.borderWidth = 1.5;
// Set image corner radius
imgView.layer.cornerRadius = 5.0;
// To enable corners to be "clipped"
[imgView setClipsToBounds:YES];
I have a code that adds a new image, with different size to a UIimageView. How would I add an image? Is that in a subclass, or viewController, or model?
this would be the code in swift:
override func viewDidLoad() {
super.viewDidLoad()
// Define a new image object
let image = UIImage(named: "massage.jpg")
// Define a new image view
let imgView = UIImageView(frame: CGRect(x: 40, y: 40, width: 157, height: 125))
// Set image in imageview and add to view
imgView.image = image
self.view.addSubview(imgView)
// Create a white border with defined width
imgView.layer.borderColor = UIColor.redColor().CGColor
imgView.layer.borderWidth = 1.5
imgView.layer.cornerRadius = 5.0;
imgView.layer.masksToBounds = true
// Do any additional setup after loading the view.
}
this is how it would look like:
viewDidLoad is called when the controller loads it's view, which is not necessarily right after initialisation.
viewWillAppear is called just before the view is displayed.viewWillAppear is called every time the view is displayed;

Setting the size for an image in a TableViewCell

I have a table with rows of height 90.
I want to have an image on each row of size 50x50.
I am doing the following in cellForRowAtIndexPath:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
if (item.image){
cell.imageView.image = [item.image smallImage];
cell.imageView.alpha = 1.0f;
} else {
cell.imageView.image = [UIImage imageNamed:#"icon-greyed.png"];
cell.imageView.alpha = 0.25f;
}
However the images appear with height 90 (the height of the row). What am I not doing right?
You will need to either add a UIImageView to cell.contentView which I wouldn't recommend, or create a custom UITableViewCell, which I would recommend. Then you can add a UIImageView to your custom cell and resize it to the dimensions you require.

iOS - Add Badge to UITableViewCell

How do I add a badge to UITableViewCell, like this:
alt text http://img17.imageshack.us/img17/9974/img0001ac9.png
Should I simply add a subview with a text and label on it?
Here's a swift enhancement to #POF's answer. We don't need as many subviews and we can use math to support N digits, not just 1-3:
func setDiscountBadge(count: Int) {
let size: CGFloat = 26
let digits = CGFloat( count("\(number)") ) // digits in the label
let width = max(size, 0.7 * size * digits) // perfect circle is smallest allowed
let badge = UILabel(frame: CGRectMake(0, 0, width, size))
badge.text = "\(number)"
badge.layer.cornerRadius = size / 2
badge.layer.masksToBounds = true
badge.textAlignment = .Center
badge.textColor = UIColor.whiteColor()
badge.backgroundColor = cfg.UIColors.brand
YOURCELL.accessoryView = badge // !! change this line
}
And the result (I use a brand color, but yours can be any color):
TDBadgedCell is a pretty good choice. Highly customizable for your needs.
As for me the simplest way is to use cell.accessoryView. Please look into my code how I did it:
UIImageView * commentsViewBG = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"counter1.png"]];
commentsViewBG.frame = CGRectMake(
commentsViewBG.frame.origin.x,
commentsViewBG.frame.origin.y, 30, 20);
UILabel *commentsCount;
if (commentsArray.totalCount < 10)
commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(10, -10, 40, 40)];
else if (commentsArray.totalCount < 100)
commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(5, -10, 40, 40)];
else if (commentsArray.totalCount < 1000)
{
commentsViewBG.frame = CGRectMake(
commentsViewBG.frame.origin.x,
commentsViewBG.frame.origin.y, 40, 20);
commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(5, -10, 40, 40)];
}
commentsCount.text = [NSString stringWithFormat:#"%ld",(long)commentsArray.totalCount];
commentsCount.textColor = [UIColor whiteColor];
commentsCount.backgroundColor = [UIColor clearColor];
[commentsViewBG addSubview:commentsCount];
cell.accessoryView = commentsViewBG;
And my result:
Hope it helps.
Yes, there is currently no supported way of adding a badge to a UITableView Cell. In this example, it is most likely a custom subview which contains an image and UILabel.
I'd like to add another alternative to create customized Badges. CustomBadge is a little bit more powerful. It's open and for free.

Resources