How do you center a UIImageView in UITableViewCell regardless of orientation?
Asked Answered
M

4

8

I'm trying to center a UIImageView width-wise in a UITableViewCell with the following code:

        // self.cover_image is a UIImage variable
        // cell is a UITableCellView

        UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image] autorelease];
        [backcover_imageview sizeToFit];

        //center image
        //float xPos = cell.contentView.frame.size.width - (self.cover_image.size.width/2);

        //TODO: need better solution
        //Eyeballing attempt:
        float xPos = self.cover_image.size.width - 25;
        CGRect bookcover_frame = backcover_imageview.frame;
        bookcover_frame.origin.x = xPos;
        backcover_imageview.frame = bookcover_frame;
        backcover_imageview.tag = 9000;
        backcover_imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

        [cell.contentView addSubview:backcover_imageview];

I'm trying to center the UIImageView in a UITableCellView regardless of what orientation the iPad or iPhone device is in. Does anyone know the right way to do this?

Manchukuo answered 17/6, 2011 at 5:33 Comment(0)
O
7

After you add backcover_imageView to the contentView of the cell, use this to center it:

imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
Officialese answered 17/6, 2011 at 5:41 Comment(2)
I opted for backcover_imageview.center = CGPointMake(cell.contentView.bounds.size.width/2,backcover_imageview.center.y); which worked for me. Thanks for your suggestion!Manchukuo
'CGPointMake' is unavailable in Swift. May be this is not working in current version of Swift. Solution is to use CGPoint as pointed out here : https://mcmap.net/q/459209/-39-cgpointmake-39-is-unavailable-in-swift-duplicateCarinacarinate
S
13

I added two more autoresizing masks and got a good result using your code and PengOne's answer:

playImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:playImage];
playImage.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
Spivey answered 7/1, 2013 at 16:19 Comment(0)
O
7

After you add backcover_imageView to the contentView of the cell, use this to center it:

imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
Officialese answered 17/6, 2011 at 5:41 Comment(2)
I opted for backcover_imageview.center = CGPointMake(cell.contentView.bounds.size.width/2,backcover_imageview.center.y); which worked for me. Thanks for your suggestion!Manchukuo
'CGPointMake' is unavailable in Swift. May be this is not working in current version of Swift. Solution is to use CGPoint as pointed out here : https://mcmap.net/q/459209/-39-cgpointmake-39-is-unavailable-in-swift-duplicateCarinacarinate
H
1

Can you try assigning the center property of cell to that of image view?

Something like this

imageView.center = cell.center;

And then add it to the cell.

Halation answered 17/6, 2011 at 5:37 Comment(0)
C
0

You can use center anchors of the cell to keep the image in the center:

image.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
Commie answered 5/6, 2019 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.