Add Size instead of Margins
Hackmodford opened this issue · 0 comments
Hackmodford commented
I've setup my code to basically allow me to set the size of the panel and have it figure out the correct margins and resize accordingly based on rotation. If anyone wants to add this feel free to use my work ;) The code below is the setup I used for my custom UAModalPanel regarding size.
- (id)initWithFrame:(CGRect)frame withimage:(UIImage *)image {
if ((self = [super initWithFrame:frame])) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
float contentWidth = 250;
float contentHeight = 250;
float xCenter = (frame.size.width / 2);
float yCenter = (frame.size.height / 2);
float topEdge = 0;
float bottomEdge = 0;
float leftEdge = 0;
float rightEdge = 0;
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) {
contentWidth = 500;
contentHeight = 500;
}
MA_MobileAppDelegate *appDelegate = (MA_MobileAppDelegate *)[[UIApplication sharedApplication] delegate];
UIInterfaceOrientation orientation = appDelegate.tabBarController.interfaceOrientation;
switch (orientation) {
case UIDeviceOrientationPortrait:
NSLog(@"Initial right side up");
topEdge = frame.size.height - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frame.size.width - (xCenter + (contentWidth / 2));
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"Initial upsideDown");
topEdge = frame.size.height - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frame.size.width - (xCenter + (contentWidth / 2));
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"Initial right");
topEdge = frame.size.height - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frame.size.width - (xCenter + (contentWidth / 2));
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"Initial Left");
topEdge = frame.size.height - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frame.size.width - (xCenter + (contentWidth / 2));
break;
default:
break;
}
self.margin = UIEdgeInsetsMake(topEdge, leftEdge, bottomEdge, rightEdge);
self.padding = UIEdgeInsetsMake(20.0, 20.0, 20.0, 20.0);
self.borderColor = [UIColor whiteColor];
self.borderWidth = 1.5f;
self.cornerRadius = 4.0f;
self.contentColor = [UIColor colorWithWhite:0.0 alpha:0.8];
self.shouldBounce = YES;
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[imageView setImage:image];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
view = imageView;
[self.contentView addSubview:view];
}
return self;
}
- (void) didRotate:(NSNotification *)notification {
MA_MobileAppDelegate *appDelegate = (MA_MobileAppDelegate *)[[UIApplication sharedApplication] delegate];
UIInterfaceOrientation orientation = appDelegate.tabBarController.interfaceOrientation;
if (currentOrientation != orientation) {
float contentWidth = 250;
float contentHeight = 250;
frameWidth = self.contentContainer.frame.size.width;
frameHeight = self.contentContainer.frame.size.height;
float xCenter = (frameWidth / 2);
float yCenter = (frameHeight / 2);
float topEdge = 0;
float bottomEdge = 0;
float leftEdge = 0;
float rightEdge = 0;
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) {
contentWidth = 500;
contentHeight = 500;
}
switch (orientation) {
case UIDeviceOrientationPortrait:
topEdge = frameHeight - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frameWidth - (xCenter + (contentWidth / 2));
currentOrientation = orientation;
break;
case UIDeviceOrientationPortraitUpsideDown:
topEdge = frameHeight - (yCenter + (contentHeight / 2));
bottomEdge = (yCenter - (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frameWidth - (xCenter + (contentWidth / 2));
currentOrientation = orientation;
break;
case UIDeviceOrientationLandscapeRight:
topEdge = (yCenter - (contentHeight / 2));
bottomEdge = frameHeight - (yCenter + (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frameWidth - (xCenter + (contentWidth / 2));
currentOrientation = orientation;
break;
case UIDeviceOrientationLandscapeLeft:
topEdge = (yCenter - (contentHeight / 2));
bottomEdge = frameHeight - (yCenter + (contentHeight / 2));
leftEdge = (xCenter - (contentWidth / 2));
rightEdge = frameWidth - (xCenter + (contentWidth / 2));
currentOrientation = orientation;
break;
default:
break;
}
self.margin = UIEdgeInsetsMake(topEdge, leftEdge, bottomEdge, rightEdge);
[self setNeedsLayout];
}
}