Search code examples
iosiphonexcodecgrectmake

xCode #if #define iphone model


In an xcode project I use (in .m file)

#define embedRectIphone CGRectMake(0, 0, 320, 180)

Which was fine in earlier versions.

But this should be different now as iPhones have many sizes so I need something like

.h file looks like :

#import <UIKit/UIKit.h>

@interface WebcamsDetailViewController : GAITrackedViewController <VideoPlayerDelegate> {

    VideoPlayerKit *videoPlayer;
    NSMutableArray *viewControllers;
    CGRect embedViewRect;

}

@end

Then, I call my element (embbeded custom video player) like this (in .m file)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        embedViewRect = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? embedRectIphone : embedRectIpad;
    }
    return self;

}

It does not seem to work on simulator. Would that be possible ? Am I wrong here ? Any other idea on how to get this fixed ?

After some help below, .m file looks like :

    #import "WebcamsDetailViewController.h"
    #import "AppDelegate.h"
    #import "Webcam.h"

    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

#define embedRectIphone IS_IPHONE_6? CGRectMake(0, 0, 377, 210):CGRectMake(0, 0, 320, 180)

    #define embedRectIpad CGRectMake(0, 0, 768, 400)

    @interface WebcamsDetailViewController ()

    @end


    @implementation WebcamsDetailViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            embedViewRect = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? embedRectIphone : embedRectIpad;
        }
        return self;

    }

    ...

    @end

Hoping for answers, Best regards, David


Solution

  • Donn't rely on these "TARGET_DEVICE_IPHONE_6" macros. Try like this.

    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
    
    #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
    #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
    #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
    #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
    
    #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
    #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
    #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
    #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
    

    then

    #define embedRectIphone IS_IPHONE_6? CGRectMake(0, 0, 377, 210):CGRectMake(0, 0, 320, 180)