user2820855
user2820855

Reputation: 1229

Change drawRect output depending on device orientation

I want to use drawRect to draw 6 boxes which will be used as the background for the output of 6 labels I have setup - when the device's orientation is changed I need to change where the boxes are drawn... I was thinking of doing an if-else statement like below:

- (void)drawRect:(CGRect)rect
{
// Drawing code
if (UIInterfaceOrientationIsPortrait(orientation)) {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.611, 0.505, 0.321, 1.0); // Goldie brown colour
    CGContextFillRect(context, CGRectMake(70, 277, 35, 50)); // Years
    CGContextFillRect(context, CGRectMake(145, 277, 35, 50)); // Months
    CGContextFillRect(context, CGRectMake(220, 277, 35, 50)); // Days
    CGContextFillRect(context, CGRectMake(70, 393, 35, 50)); // Hours
    CGContextFillRect(context, CGRectMake(145, 393, 35, 50)); // Minutes
    CGContextFillRect(context, CGRectMake(220, 393, 35, 50)); // Seconds
} else {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.611, 0.505, 0.321, 1.0); // Goldie brown colour
    CGContextFillRect(context, CGRectMake(87, 221, 35, 50)); // Years
    CGContextFillRect(context, CGRectMake(162, 221, 35, 50)); // Months
    CGContextFillRect(context, CGRectMake(234, 221, 35, 50)); // Days
    CGContextFillRect(context, CGRectMake(308, 221, 35, 50)); // Hours
    CGContextFillRect(context, CGRectMake(385, 221, 35, 50)); // Minutes
    CGContextFillRect(context, CGRectMake(466, 221, 35, 50)); // Seconds
 }

but this wont work because (UIInterfaceOrientationIsPortrait(orientation)) needs a parameter/constant declaring & it doesnt look like I can do that here - is there something similar I can do?

Upvotes: 0

Views: 285

Answers (2)

Juanpe Catalan
Juanpe Catalan

Reputation: 1

When will device change the orientation, in didRotateFromInterfaceOrientation, you must call at method setNeedDisplay of UIView.

ex: [box1 setNeedDisplay];

Upvotes: 0

gWiz
gWiz

Reputation: 1284

You can either do it by using

if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))

or by changing a flag (e.g. BOOL shouldDrawPortrait) in didRotateFromInterfaceOrientation: of your view controller.

Upvotes: 1

Related Questions