Search code examples
iosuiviewcontrollercgrect

Creating image frame not displaying in simulator


I have an app that captures the users signature, myviewcontroller displays the signature capture. I'm attempting to create a rectangle in the center of the screen that is centered for this to happen using CGRect. When I run this I get no errors, but I get the picture below and I can't figure out what is causing this. Within the CGRect code, I am trying to have the rectangle that is created be centered in the middle of the screen, and the target device is ipad. I've posted both the .h and .m files, the .m file is incomplete as the rest has to do with the code used to capture the signature then save it.

Any help would be appreciated. Simulation

//MyViewController.h

    #import <UIKit/UIKit.h>
    #import <CoreData/CoreData.h>
    #import <Foundation/Foundation.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 (nonatomic, assign) UIImageView* imageView;


    @property (nonatomic, retain) NSData*image;
    @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

    //Save and cancel buttons

    - (IBAction)cancel:(id)sender;
    - (IBAction)save:(id)sender;


    @end

//MyViewController.m

#import "MyViewController.h"



@interface MyViewController ()

@end

@implementation MyViewController

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


- (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 imageFrame = CGRectMake(
                                   CGRectGetMidX(self.view.frame) - CGRectGetMidX(imageView.frame),
                                   CGRectGetMidY(self.view.frame) - CGRectGetMidY(imageView.frame),
                                   CGRectGetWidth(imageView.frame),
                                   CGRectGetHeight(imageView.frame));

//    //allocate an image view and add to the main view
    mySignatureImage = [[UIImageView alloc] initWithImage:nil];
    mySignatureImage.frame = imageFrame;
    mySignatureImage.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mySignatureImage];


}

Solution

  • Move the code (except for [super viewDidLoad]) in viewDidLoad to a later stage, such as viewWillAppear. During viewDidLoad the layout of the views is not yet complete so their bounds/frames are not yet set to their final values.