CGRect Center for Ipad
Asked Answered
H

5

7

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;
Hearing answered 6/8, 2014 at 9:53 Comment(7)
How big do you want the image frame to be?Tusche
imageView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));Peccadillo
It's meant to be big enough for a user to write a signature, so pretty big. I've played with the numbers and sizes, but centering is what's causing issues. @TuscheHearing
@Peccadillo Thanks for your response, tried that and I'm getting an Unknown type name 'imageView" did you mean UIIMageView errorHearing
@Hearing imageView is UIImageView.Peccadillo
@Peccadillo Thanks! one more question, this will work for an ipad right? or is it just for mac osxHearing
It will work for both.Peccadillo
T
11

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));
Tusche answered 6/8, 2014 at 9:59 Comment(6)
Thanks for your comment, when I try this I get a "Property 'size' not found on object of type 'NSData'Hearing
@Hearing My answer assumes image is an UIImage object. You didn't show what the image was in your question so you'll have to expand the code in your question in order for me to answer more completely.Tusche
I've updated my code snippet above for your reference, thank youHearing
Thank you for updating, so when I make thise changes, I get a semantic error, that says no member named frame in struct cgrectHearing
Not anymore, thanks for the update it compiles but when i run the simulator I dont see the rectange it's making at allHearing
@Hearing OK, post another question if you cannot solve that issue.Tusche
L
6

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));
Luhe answered 6/8, 2014 at 10:24 Comment(2)
@holex Not really. bounds and center are what are actually used to set a view's frame. So centering an image or putting an image anywhere you want is just as easily done by setting the center property of a view to any point that you want it to be at.Luhe
if you have two views: view1 with frame (100.0, 100.0, 50.0, 30.0) and it has a subview as view2 with frame (0.0, 0.0, 50.0, 30.0) the view2.center will never be equal to the view2.superview.center... your idea works only if the superview covers the entire screen precisely – in any other cases it won't work as you'd like to.Fa
E
1
- (void)viewDidAppear:(BOOL)animated {


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

/*write code in viewDidApper */
Elicia answered 6/8, 2014 at 10:31 Comment(1)
Welcome to SO! Please add a short eplanation for your code, as well.Donella
P
0
CGPoint (^CGRectGetCenter)(CGRect) = ^(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
};

example : The above answer by Mirant

Perfection answered 8/5, 2019 at 11:49 Comment(0)
U
0
let center = CGPoint(x: rect.midX, y: rect.midY)
Undersea answered 9/3, 2021 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.