Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Fixed orientation bugs (fix #14) by not adding OLGAV as a subview of …
Browse files Browse the repository at this point in the history
…the window.

Trust me, I tried. I really tried. In the process, OLGAV got slightly more awesome by making extensive use of layoutSubviews. This change, ironically, improves OLGAV's behaviour when rotating by a lot, since it now recalculates all frames to adapt to the new size. Hooray?
  • Loading branch information
radutzan committed Apr 23, 2013
1 parent e57ec8b commit 76d0973
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 147 deletions.
274 changes: 130 additions & 144 deletions OLGhostAlertView.m
Expand Up @@ -2,7 +2,7 @@
// OLGhostAlertView.m // OLGhostAlertView.m
// //
// Originally created by Radu Dutzan. // Originally created by Radu Dutzan.
// (c) 2012 Onda. // (c) 2012-2013 Onda.
// //


#import <QuartzCore/QuartzCore.h> #import <QuartzCore/QuartzCore.h>
Expand All @@ -20,8 +20,8 @@


@interface OLGhostAlertView () @interface OLGhostAlertView ()


@property (strong, nonatomic) UILabel *title; @property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *message; @property (strong, nonatomic) UILabel *messageLabel;
@property (strong, nonatomic) UITapGestureRecognizer *dismissTap; @property (strong, nonatomic) UITapGestureRecognizer *dismissTap;
@property NSTimeInterval timeout; @property NSTimeInterval timeout;
@property UIInterfaceOrientation interfaceOrientation; @property UIInterfaceOrientation interfaceOrientation;
Expand All @@ -33,8 +33,8 @@ @interface OLGhostAlertView ()


@implementation OLGhostAlertView @implementation OLGhostAlertView


@synthesize title = _title; @synthesize titleLabel = _titleLabel;
@synthesize message = _message; @synthesize messageLabel = _messageLabel;
@synthesize dismissTap = _dismissTap; @synthesize dismissTap = _dismissTap;
@synthesize timeout = _timeout; @synthesize timeout = _timeout;
@synthesize interfaceOrientation = _interfaceOrientation; @synthesize interfaceOrientation = _interfaceOrientation;
Expand All @@ -53,28 +53,38 @@ - (id)initWithFrame:(CGRect)frame
self.backgroundColor = [UIColor colorWithWhite:0 alpha:.45]; self.backgroundColor = [UIColor colorWithWhite:0 alpha:.45];
self.alpha = 0; self.alpha = 0;


_title = [[UILabel alloc] initWithFrame:CGRectMake(HORIZONTAL_PADDING, VERTICAL_PADDING, 0, 0)]; _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(HORIZONTAL_PADDING, VERTICAL_PADDING, 0, 0)];
_title.backgroundColor = [UIColor clearColor]; _titleLabel.backgroundColor = [UIColor clearColor];
_title.textColor = [UIColor whiteColor]; _titleLabel.textColor = [UIColor whiteColor];
_title.textAlignment = NSTextAlignmentCenter; _titleLabel.textAlignment = NSTextAlignmentCenter;
_title.font = [UIFont boldSystemFontOfSize:TITLE_FONT_SIZE]; _titleLabel.font = [UIFont boldSystemFontOfSize:TITLE_FONT_SIZE];
_title.numberOfLines = 0; _titleLabel.numberOfLines = 0;
_title.lineBreakMode = NSLineBreakByWordWrapping; _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_titleLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);


[self addSubview:_title]; [self addSubview:_titleLabel];


_message = [[UILabel alloc] initWithFrame:CGRectMake(HORIZONTAL_PADDING, 0, 0, 0)]; _messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(HORIZONTAL_PADDING, 0, 0, 0)];
_message.backgroundColor = [UIColor clearColor]; _messageLabel.backgroundColor = [UIColor clearColor];
_message.textColor = [UIColor whiteColor]; _messageLabel.textColor = [UIColor whiteColor];
_message.textAlignment = NSTextAlignmentCenter; _messageLabel.textAlignment = NSTextAlignmentCenter;
_message.font = [UIFont systemFontOfSize:MESSAGE_FONT_SIZE]; _messageLabel.font = [UIFont systemFontOfSize:MESSAGE_FONT_SIZE];
_message.numberOfLines = 0; _messageLabel.numberOfLines = 0;
_message.lineBreakMode = NSLineBreakByWordWrapping; _messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
_messageLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);


_position = OLGhostAlertViewPositionBottom; _position = OLGhostAlertViewPositionBottom;


_interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
_bottomMargin = 25;
} else {
_bottomMargin = 50;
}

[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:) selector:@selector(didChangeOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil]; object:nil];


Expand All @@ -87,7 +97,6 @@ - (id)initWithFrame:(CGRect)frame
selector:@selector(keyboardWillHide:) selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification name:UIKeyboardWillHideNotification
object:nil]; object:nil];

} }
return self; return self;
} }
Expand All @@ -100,68 +109,14 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message timeout:(NSTim
return self; return self;
} }


_interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; self = [self initWithFrame:CGRectZero];

CGFloat maxWidth;
CGFloat totalLabelWidth;
CGFloat totalHeight;

CGRect screenRect = [self getScreenBoundsForCurrentOrientation];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (UIDeviceOrientationIsPortrait(_interfaceOrientation)) {
maxWidth = 280 - (HORIZONTAL_PADDING * 2);
} else {
maxWidth = 420 - (HORIZONTAL_PADDING * 2);
}
} else {
maxWidth = 520 - (HORIZONTAL_PADDING * 2);
}

CGSize constrainedSize;
constrainedSize.width = maxWidth;
constrainedSize.height = MAXFLOAT;

CGSize titleSize = [title sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constrainedSize];
CGSize messageSize = CGSizeZero;

if (message) {
messageSize = [message sizeWithFont:[UIFont systemFontOfSize:MESSAGE_FONT_SIZE] constrainedToSize:constrainedSize];

totalHeight = titleSize.height + messageSize.height + floorf(VERTICAL_PADDING * 2.5);
} else {
totalHeight = titleSize.height + floorf(VERTICAL_PADDING * 2);
}

if (titleSize.width == maxWidth || messageSize.width == maxWidth) {
totalLabelWidth = maxWidth;
} else if (messageSize.width > titleSize.width) {
totalLabelWidth = messageSize.width;
} else {
totalLabelWidth = titleSize.width;
}

CGFloat totalWidth = totalLabelWidth + (HORIZONTAL_PADDING * 2);

CGFloat xPosition = floorf((screenRect.size.width / 2) - (totalWidth / 2));

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
_bottomMargin = 25;
} else {
_bottomMargin = 50;
}

self = [self initWithFrame:CGRectMake(xPosition, screenRect.size.height - totalHeight - _bottomMargin, totalWidth, totalHeight)];

if (self) { if (self) {
_title.text = title; _titleLabel.text = title;
_title.frame = CGRectMake(_title.frame.origin.x, _title.frame.origin.y, totalLabelWidth, titleSize.height);


if (message) { if (message) {
_message.text = message; _messageLabel.text = message;
_message.frame = CGRectMake(_message.frame.origin.x, titleSize.height + floorf(VERTICAL_PADDING * 1.5), totalLabelWidth, messageSize.height);


[self addSubview:_message]; [self addSubview:_messageLabel];
} }


_timeout = timeout; _timeout = timeout;
Expand Down Expand Up @@ -195,15 +150,22 @@ - (id)initWithTitle:(NSString *)title
- (void)show - (void)show
{ {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window]; UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIView *parentView;


for (UIView *subView in [window subviews]) { if (window.rootViewController.presentedViewController) {
parentView = window.rootViewController.presentedViewController.view;
} else {
parentView = window.rootViewController.view;
}

for (UIView *subView in [parentView subviews]) {
if ([subView isKindOfClass:[OLGhostAlertView class]]) { if ([subView isKindOfClass:[OLGhostAlertView class]]) {
OLGhostAlertView *otherOLGAV = (OLGhostAlertView *)subView; OLGhostAlertView *otherOLGAV = (OLGhostAlertView *)subView;
[otherOLGAV hide]; [otherOLGAV hide];
} }
} }


[window addSubview:self]; [parentView addSubview:self];


[UIView animateWithDuration:0.5 animations:^{ [UIView animateWithDuration:0.5 animations:^{
self.alpha = 1; self.alpha = 1;
Expand All @@ -227,13 +189,84 @@ - (void)hide
}]; }];
} }


#pragma mark - Handle changes to viewport #pragma mark - Calculate and change view frames


- (void)didRotate:(NSNotification *)notification - (void)layoutSubviews
{ {
self.interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; CGFloat maxWidth;
CGFloat totalLabelWidth;
CGFloat totalHeight;

CGRect screenRect = [self superviewBoundsForCurrentOrientation];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) {
maxWidth = 280 - (HORIZONTAL_PADDING * 2);
} else {
maxWidth = 420 - (HORIZONTAL_PADDING * 2);
}
} else {
maxWidth = 520 - (HORIZONTAL_PADDING * 2);
}


[self didChangeScreenBounds]; CGSize constrainedSize;
constrainedSize.width = maxWidth;
constrainedSize.height = MAXFLOAT;

CGSize titleSize = [self.titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constrainedSize];
CGSize messageSize = CGSizeZero;

if (self.messageLabel.text) {
messageSize = [self.messageLabel.text sizeWithFont:[UIFont systemFontOfSize:MESSAGE_FONT_SIZE] constrainedToSize:constrainedSize];

totalHeight = titleSize.height + messageSize.height + floorf(VERTICAL_PADDING * 2.5);

} else {
totalHeight = titleSize.height + floorf(VERTICAL_PADDING * 2);
}

if (titleSize.width == maxWidth || messageSize.width == maxWidth) {
totalLabelWidth = maxWidth;

} else if (messageSize.width > titleSize.width) {
totalLabelWidth = messageSize.width;

} else {
totalLabelWidth = titleSize.width;
}

CGFloat totalWidth = totalLabelWidth + (HORIZONTAL_PADDING * 2);

CGFloat xPosition = floorf((screenRect.size.width / 2) - (totalWidth / 2));

CGFloat yPosition;

switch (self.position) {
case OLGhostAlertViewPositionBottom:
default:
yPosition = screenRect.size.height - totalHeight - self.bottomMargin;
break;

case OLGhostAlertViewPositionCenter:
yPosition = ceilf((screenRect.size.height / 2) - (totalHeight / 2));
break;

case OLGhostAlertViewPositionTop:
yPosition = self.bottomMargin;
break;
}

self.frame = CGRectMake(xPosition, yPosition, totalWidth, totalHeight);

if (self.isShowingKeyboard && self.position == OLGhostAlertViewPositionBottom) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y - self.keyboardHeight, self.frame.size.width, self.frame.size.height);
}

self.titleLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y, totalLabelWidth, titleSize.height);

if (self.messageLabel) {
self.messageLabel.frame = CGRectMake(self.messageLabel.frame.origin.x, titleSize.height + floorf(VERTICAL_PADDING * 1.5), totalLabelWidth, messageSize.height);
}
} }


- (void)keyboardWillShow:(NSNotification*)notification - (void)keyboardWillShow:(NSNotification*)notification
Expand All @@ -244,82 +277,35 @@ - (void)keyboardWillShow:(NSNotification*)notification
self.isShowingKeyboard = YES; self.isShowingKeyboard = YES;
self.keyboardHeight = keyboardSize.height; self.keyboardHeight = keyboardSize.height;


[self didChangeScreenBounds]; [self setNeedsLayout];
} }


- (void)keyboardWillHide:(NSNotification*)notification - (void)keyboardWillHide:(NSNotification*)notification
{ {
self.isShowingKeyboard = NO; self.isShowingKeyboard = NO;


[self didChangeScreenBounds]; [self setNeedsLayout];
}

- (void)didChangeScreenBounds
{
CGRect screenRect = [self getScreenBoundsForCurrentOrientation];

self.frame = CGRectMake(floorf((screenRect.size.width / 2) - (self.frame.size.width / 2)), self.frame.origin.y, self.frame.size.width, self.frame.size.height);

OLGhostAlertViewPosition storedPosition = self.position;
_position = 8;
self.position = storedPosition;

if (self.isShowingKeyboard && self.position == OLGhostAlertViewPositionBottom) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y - self.keyboardHeight, self.frame.size.width, self.frame.size.height);
}

[UIView animateWithDuration:0.25 animations:^{
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}];
} }


#pragma mark - Orientation helper methods #pragma mark - Orientation helper methods


- (CGRect)getScreenBoundsForCurrentOrientation - (void)didChangeOrientation:(NSNotification *)notification
{ {
return [self getScreenBoundsForOrientation:self.interfaceOrientation]; self.interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

[self setNeedsLayout];
} }


- (CGRect)getScreenBoundsForOrientation:(UIInterfaceOrientation)orientation - (CGRect)superviewBoundsForCurrentOrientation
{ {
UIScreen *screen = [UIScreen mainScreen]; return [self screenBoundsForOrientation:self.interfaceOrientation];
CGRect screenRect = screen.bounds; // implicitly in Portrait orientation.

if (UIDeviceOrientationIsLandscape(orientation)) {
CGRect temp = CGRectMake(0, 0, screenRect.size.height, screenRect.size.width);
screenRect = temp;
}

return screenRect;
} }


#pragma mark - Position setter - (CGRect)screenBoundsForOrientation:(UIInterfaceOrientation)orientation

- (void)setPosition:(OLGhostAlertViewPosition)position
{ {
if (_position == position) return; CGRect screenRect = self.superview.bounds;

_position = position;

CGRect screenRect = [self getScreenBoundsForCurrentOrientation];

CGFloat yPosition;

switch (position) {
case OLGhostAlertViewPositionBottom:
yPosition = screenRect.size.height - self.frame.size.height - self.bottomMargin;
break;

case OLGhostAlertViewPositionCenter:
yPosition = ceilf((screenRect.size.height / 2) - (self.frame.size.height / 2)) + 10;
break;

case OLGhostAlertViewPositionTop:
yPosition = self.bottomMargin + 20;
break;
}


self.frame = CGRectMake(self.frame.origin.x, yPosition, self.frame.size.width, self.frame.size.height); return screenRect;
} }


#pragma mark - dealloc #pragma mark - dealloc
Expand Down
4 changes: 2 additions & 2 deletions OLGhostAlertView.podspec
@@ -1,12 +1,12 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'OLGhostAlertView' s.name = 'OLGhostAlertView'
s.version = '1.2.1' s.version = '1.3'
s.license = 'Public Domain' s.license = 'Public Domain'
s.platform = :ios s.platform = :ios
s.summary = 'Temporary and unobtrusive translucent alert view for iPhone and iPad.' s.summary = 'Temporary and unobtrusive translucent alert view for iPhone and iPad.'
s.homepage = 'https://github.com/ondalabs/OLGhostAlertView' s.homepage = 'https://github.com/ondalabs/OLGhostAlertView'
s.authors = { 'Radu Dutzan' => 'radu@ondalabs.com'} s.authors = { 'Radu Dutzan' => 'radu@ondalabs.com'}
s.source = { :git => 'https://github.com/ondalabs/OLGhostAlertView.git', :tag => '1.2.1' } s.source = { :git => 'https://github.com/ondalabs/OLGhostAlertView.git', :tag => '1.3' }
s.source_files = 'OLGhostAlertView.{h,m}' s.source_files = 'OLGhostAlertView.{h,m}'
s.requires_arc = true s.requires_arc = true


Expand Down
Expand Up @@ -37,7 +37,7 @@ - (void)viewDidAppear:(BOOL)animated
OLGhostAlertView *demo3 = [[OLGhostAlertView alloc] initWithTitle:@"Check out the code." message:@"Try out different setups before implementing it in your app."]; OLGhostAlertView *demo3 = [[OLGhostAlertView alloc] initWithTitle:@"Check out the code." message:@"Try out different setups before implementing it in your app."];
demo3.completionBlock = ^(void) { demo3.completionBlock = ^(void) {


OLGhostAlertView *demo4 = [[OLGhostAlertView alloc] initWithTitle:@"Have fun!" message:@"You can tap this message to dismiss it." timeout:100.0 dismissible:YES]; OLGhostAlertView *demo4 = [[OLGhostAlertView alloc] initWithTitle:@"Have fun!" message:@"You can tap this message to dismiss it." timeout:900.0 dismissible:YES];
demo4.position = OLGhostAlertViewPositionCenter; demo4.position = OLGhostAlertViewPositionCenter;
[demo4 show]; [demo4 show];


Expand Down

0 comments on commit 76d0973

Please sign in to comment.