Skip to content

Commit

Permalink
Added logic to prevent UIAlertView collapsing in landscape mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Apr 3, 2012
1 parent 12efb29 commit 910670a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 50 deletions.
5 changes: 5 additions & 0 deletions Examples/iPhone Demo/Classes/iNotifyViewController.m
Expand Up @@ -10,4 +10,9 @@

@implementation iNotifyViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}

@end
2 changes: 1 addition & 1 deletion LICENCE.md
@@ -1,6 +1,6 @@
iNotify

Version 1.5, March 16th, 2012
Version 1.5.1, April 4th, 2012

Copyright (C) 2011 Charcoal Design

Expand Down
4 changes: 4 additions & 0 deletions RELEASE NOTES.md
@@ -1,3 +1,7 @@
Version 1.5.1

- Added logic to prevent UIAlertView collapsing in landscape mode

Version 1.5

- Included localisation for French, German, Italian, Spanish and Japanese
Expand Down
4 changes: 3 additions & 1 deletion iNotify/iNotify.h
@@ -1,7 +1,7 @@
//
// iNotify.h
//
// Version 1.5
// Version 1.5.1
//
// Created by Nick Lockwood on 26/01/2011.
// Copyright 2011 Charcoal Design
Expand Down Expand Up @@ -138,6 +138,8 @@ static NSString *const iNotifyMessageMaxVersionKey = @"MaxVersion";
BOOL checkAtLaunch;
BOOL debug;
id<iNotifyDelegate> __AH_WEAK delegate;
id visibleAlert;
NSString *message;
}
#endif

Expand Down
156 changes: 108 additions & 48 deletions iNotify/iNotify.m
@@ -1,7 +1,7 @@
//
// iNotify.m
//
// Version 1.5
// Version 1.5.1
//
// Created by Nick Lockwood on 26/01/2011.
// Copyright 2011 Charcoal Design
Expand Down Expand Up @@ -54,6 +54,8 @@ @interface iNotify()

@property (nonatomic, copy) NSDictionary *notificationsDict;
@property (nonatomic, strong) NSError *downloadError;
@property (nonatomic, strong) id visibleAlert;
@property (nonatomic, strong) NSString *message;

@end

Expand All @@ -75,7 +77,8 @@ @implementation iNotify
@synthesize checkAtLaunch;
@synthesize debug;
@synthesize delegate;

@synthesize visibleAlert;
@synthesize message;

+ (iNotify *)sharedInstance
{
Expand Down Expand Up @@ -134,6 +137,11 @@ - (iNotify *)init
name:UIApplicationWillEnterForegroundNotification
object:nil];
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate)
name:UIDeviceOrientationDidChangeNotification
object:nil];
#else
//register for mac application events
[[NSNotificationCenter defaultCenter] addObserver:self
Expand Down Expand Up @@ -267,6 +275,8 @@ - (void)dealloc
AH_RELEASE(ignoreButtonLabel);
AH_RELEASE(remindButtonLabel);
AH_RELEASE(defaultActionButtonLabel);
AH_RELEASE(visibleAlert);
AH_RELEASE(message);
AH_SUPER_DEALLOC;
}

Expand Down Expand Up @@ -368,7 +378,7 @@ - (void)downloadedNotificationsData

//get notification details
NSString *title = [notification objectForKey:iNotifyTitleKey];
NSString *message = [notification objectForKey:iNotifyMessageKey];
NSString *_message = [notification objectForKey:iNotifyMessageKey];
NSString *actionURL = [notification objectForKey:iNotifyActionURLKey];
NSString *actionButtonLabel = [notification objectForKey:iNotifyActionButtonKey] ?: defaultActionButtonLabel;

Expand All @@ -381,54 +391,59 @@ - (void)downloadedNotificationsData
}
}

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
if (actionURL)
if (!visibleAlert)
{
[alert addButtonWithTitle:actionButtonLabel];
[alert addButtonWithTitle:remindButtonLabel];
[alert addButtonWithTitle:ignoreButtonLabel];
alert.cancelButtonIndex = 2;
}
else
{
[alert addButtonWithTitle:okButtonLabel];
alert.cancelButtonIndex = 0;
}
self.message = _message;

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

[alert show];
AH_RELEASE(alert);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
if (actionURL)
{
[alert addButtonWithTitle:actionButtonLabel];
[alert addButtonWithTitle:remindButtonLabel];
[alert addButtonWithTitle:ignoreButtonLabel];
alert.cancelButtonIndex = 2;
}
else
{
[alert addButtonWithTitle:okButtonLabel];
alert.cancelButtonIndex = 0;
}

self.visibleAlert = alert;
[visibleAlert show];
AH_RELEASE(alert);

#else
NSAlert *alert = nil;

if (actionURL)
{
alert = [NSAlert alertWithMessageText:title
defaultButton:actionButtonLabel
alternateButton:ignoreButtonLabel
otherButton:remindButtonLabel
informativeTextWithFormat:message];
}
else
{
alert = [NSAlert alertWithMessageText:title
defaultButton:okButtonLabel
alternateButton:nil
otherButton:nil
informativeTextWithFormat:message];
}

[alert beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];

if (actionURL)
{
self.visibleAlert = [NSAlert alertWithMessageText:title
defaultButton:actionButtonLabel
alternateButton:ignoreButtonLabel
otherButton:remindButtonLabel
informativeTextWithFormat:message];
}
else
{
self.visibleAlert = [NSAlert alertWithMessageText:title
defaultButton:okButtonLabel
alternateButton:nil
otherButton:nil
informativeTextWithFormat:message];
}

[visibleAlert beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
#endif

}
}
}

Expand Down Expand Up @@ -500,10 +515,52 @@ - (void)checkForNotifications
}

#pragma mark -
#pragma mark UIAlertViewDelegate methods
#pragma mark UIAlertView methods

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

- (void)resizeAlert
{
CGFloat offset = 0.0f;
UIAlertView *alertView = visibleAlert;
for (UIView *view in alertView.subviews)
{
CGRect frame = view.frame;
if ([view isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)view;
if ([label.text isEqualToString:self.message])
{
label.alpha = 1.0f;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit];
offset = label.frame.size.height - frame.size.height;
frame.size.height = label.frame.size.height;
}
}
else if ([view isKindOfClass:[UIControl class]])
{
frame.origin.y += offset;
}
view.frame = frame;
}
CGRect frame = alertView.frame;
frame.origin.y -= roundf(offset/2.0f);
frame.size.height += offset;
alertView.frame = frame;
}

- (void)didRotate
{
[self performSelectorOnMainThread:@selector(resizeAlert) withObject:nil waitUntilDone:NO];
}

- (void)willPresentAlertView:(UIAlertView *)alertView
{
[self resizeAlert];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString *key = [self nextNotificationInDict:notificationsDict];
Expand Down Expand Up @@ -558,6 +615,9 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
//open URL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:actionURL]];
}

//release alert
self.visibleAlert = nil;
}

#else
Expand Down

0 comments on commit 910670a

Please sign in to comment.