aramirez
aramirez

Reputation: 87

CGRect Center for Ipad

I am creating an image frame using CGRect. I want to center the rectangle that is created.

I've looked on here, as well as in the Apple documentation for the best way to do this, and found CGRectGetMidY and CGRectGetMidX which get the center coordinates.

When I try implementing this into my own code I run into problems. I get a Property size not found on object of type UIIMageView error

       #import "MyViewController.h"



    @interface MyViewController ()

    @end

    @implementation MyViewController

    @synthesize mySignatureImage;
    @synthesize lastContactPoint1, lastContactPoint2, currentPoint;
    @synthesize imageFrame;
    @synthesize fingerMoved;
    @synthesize navbarHeight;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];



        self.view.backgroundColor = [UIColor lightGrayColor];

        CGRect mySignatureImageFrame = CGRectMake(
                                       CGRectGetMidX(self.view.frame) - (mySignatureImage.size.width/ 2.0),
                                       CGRectGetMidY(self.view.frame) - (mySignatureImage.size.height / 2.0),
                                       image.size.width,
                                       image.size.height);




#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UIAlertViewDelegate>

@property (nonatomic, strong) UIImageView *mySignatureImage;
@property (nonatomic, assign) CGPoint lastContactPoint1, lastContactPoint2, currentPoint;
@property (nonatomic, assign) CGRect imageFrame;
@property (nonatomic, assign) BOOL fingerMoved;
@property (nonatomic, assign) float navbarHeight;


@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

Upvotes: 7

Views: 11905

Answers (5)

Anton Tropashko
Anton Tropashko

Reputation: 5816

let center = CGPoint(x: rect.midX, y: rect.midY)

Upvotes: 0

Suraj K Thomas
Suraj K Thomas

Reputation: 5883

CGPoint (^CGRectGetCenter)(CGRect) = ^(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
};

example : The above answer by Mirant

Upvotes: 0

Anorak
Anorak

Reputation: 3116

You don't need to calculate the centre point, as it's available for a view anyway:

CGRect superviewBounds = superview.bounds;
imageView.center = CGPointMake(CGRectGetMidX(superviewBounds), CGRectGetMidY(superviewBounds));

Upvotes: 6

Mirant
Mirant

Reputation: 317

- (void)viewDidAppear:(BOOL)animated {


img.center = CGPointMake(CGRectGetMidX([self.view bounds]), CGRectGetMidY([self.view   bounds]));
}

/*write code in viewDidApper */

Upvotes: 1

Droppy
Droppy

Reputation: 9731

Assuming image is of type UIImage then:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - (image.size.width / 2.0),
    CGRectGetMidY(self.view.frame) - (image.size.height / 2.0),
    image.size.width,
    image.size.height);

Assuming imageView is of type UIImageView then:

CGRect imageFrame = CGRectMake(
    CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
    CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
    CGRectGetWidth(imageView.frame),
    CGRectGetHeight(imageView.frame));

Upvotes: 11

Related Questions