From 2bd05b5d8310fbac1456791a002b4a03510dec30 Mon Sep 17 00:00:00 2001 From: Werner Altewischer Date: Sat, 7 May 2011 23:22:15 +0200 Subject: [PATCH] Fixed support for interface rotation --- Classes/UIBarButtonItem+WEPopover.h | 1 + Classes/UIBarButtonItem+WEPopover.m | 45 +++++++++++++------- Classes/WEPopoverController.h | 3 +- Classes/WEPopoverController.m | 57 ++++++++++++++++++++------ Classes/WEPopoverTableViewController.h | 3 +- Classes/WEPopoverTableViewController.m | 13 +++--- Classes/WETouchableView.m | 4 +- MainWindow.xib | 57 +++++++++++++++++--------- WEPopover-Info.plist | 2 +- WEPopover.xcodeproj/project.pbxproj | 2 + 10 files changed, 130 insertions(+), 57 deletions(-) mode change 100755 => 100644 MainWindow.xib diff --git a/Classes/UIBarButtonItem+WEPopover.h b/Classes/UIBarButtonItem+WEPopover.h index 7dde636..0b8391a 100644 --- a/Classes/UIBarButtonItem+WEPopover.h +++ b/Classes/UIBarButtonItem+WEPopover.h @@ -10,5 +10,6 @@ @interface UIBarButtonItem(WEPopover) - (CGRect)frameInView:(UIView *)v; +- (UIView *)superview; @end diff --git a/Classes/UIBarButtonItem+WEPopover.m b/Classes/UIBarButtonItem+WEPopover.m index de24ada..8b33330 100644 --- a/Classes/UIBarButtonItem+WEPopover.m +++ b/Classes/UIBarButtonItem+WEPopover.m @@ -12,24 +12,41 @@ @implementation UIBarButtonItem(WEPopover) - (CGRect)frameInView:(UIView *)v { - - UIView *currentCustomView = [self.customView retain]; - UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero]; - - self.customView = tempView; - - [tempView release]; - + + BOOL hasCustomView = (self.customView != nil); + + if (!hasCustomView) { + UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero]; + self.customView = tempView; + [tempView release]; + } + UIView *parentView = self.customView.superview; - NSUInteger indexOfView = [parentView.subviews indexOfObject:self.customView]; - - self.customView = currentCustomView; - [currentCustomView release]; - + + if (!hasCustomView) { + self.customView = nil; + } UIView *button = [parentView.subviews objectAtIndex:indexOfView]; - return [parentView convertRect:button.frame toView:v]; } +- (UIView *)superview { + + BOOL hasCustomView = (self.customView != nil); + + if (!hasCustomView) { + UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero]; + self.customView = tempView; + [tempView release]; + } + + UIView *parentView = self.customView.superview; + + if (!hasCustomView) { + self.customView = nil; + } + return parentView; +} + @end diff --git a/Classes/WEPopoverController.h b/Classes/WEPopoverController.h index 374bb22..324c869 100755 --- a/Classes/WEPopoverController.h +++ b/Classes/WEPopoverController.h @@ -33,7 +33,7 @@ CGSize popoverContentSize; WEPopoverContainerViewProperties *containerViewProperties; id context; - NSArray *passthroughViews; + NSArray *passthroughViews; } @property(nonatomic, retain) UIViewController *contentViewController; @@ -61,6 +61,7 @@ animated:(BOOL)animated; - (void)repositionPopoverFromRect:(CGRect)rect + inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections; @end diff --git a/Classes/WEPopoverController.m b/Classes/WEPopoverController.m index 2359f85..ccddf17 100755 --- a/Classes/WEPopoverController.m +++ b/Classes/WEPopoverController.m @@ -14,6 +14,8 @@ @interface WEPopoverController(Private) +- (UIView *)keyView; +- (void)updateBackgroundPassthroughViews; - (void)setView:(UIView *)v; - (CGRect)displayAreaForView:(UIView *)theView; - (WEPopoverContainerViewProperties *)defaultContainerViewProperties; @@ -71,7 +73,7 @@ - (void)setPassthroughViews:(NSArray *)array { if (array) { passthroughViews = [[NSArray alloc] initWithArray:array]; } - backgroundView.passthroughViews = passthroughViews; + [self updateBackgroundPassthroughViews]; } - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)theContext { @@ -107,9 +109,7 @@ - (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated { - - UIView *v = [[UIApplication sharedApplication] keyWindow]; - + UIView *v = [self keyView]; CGRect rect = [item frameInView:v]; return [self presentPopoverFromRect:rect inView:v permittedArrowDirections:arrowDirections animated:animated]; @@ -129,23 +129,34 @@ - (void)presentPopoverFromRect:(CGRect)rect WEPopoverContainerView *containerView = [[[WEPopoverContainerView alloc] initWithSize:self.popoverContentSize anchorRect:rect displayArea:displayArea permittedArrowDirections:arrowDirections properties:props] autorelease]; popoverArrowDirection = containerView.arrowDirection; - UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; + UIView *keyView = self.keyView; - backgroundView = [[WETouchableView alloc] initWithFrame:keyWindow.bounds]; + backgroundView = [[WETouchableView alloc] initWithFrame:keyView.bounds]; + backgroundView.contentMode = UIViewContentModeScaleToFill; + backgroundView.autoresizingMask = ( UIViewAutoresizingFlexibleLeftMargin | + UIViewAutoresizingFlexibleWidth | + UIViewAutoresizingFlexibleRightMargin | + UIViewAutoresizingFlexibleTopMargin | + UIViewAutoresizingFlexibleHeight | + UIViewAutoresizingFlexibleBottomMargin); backgroundView.backgroundColor = [UIColor clearColor]; backgroundView.delegate = self; - backgroundView.passthroughViews = self.passthroughViews; - [keyWindow addSubview:backgroundView]; + [keyView addSubview:backgroundView]; containerView.frame = [theView convertRect:containerView.frame toView:backgroundView]; [backgroundView addSubview:containerView]; containerView.contentView = contentViewController.view; + containerView.autoresizingMask = ( UIViewAutoresizingFlexibleLeftMargin | + UIViewAutoresizingFlexibleRightMargin); self.view = containerView; + [self updateBackgroundPassthroughViews]; + [contentViewController viewWillAppear:animated]; + [self.view becomeFirstResponder]; if (animated) { @@ -163,15 +174,21 @@ - (void)presentPopoverFromRect:(CGRect)rect } else { popoverVisible = YES; [contentViewController viewDidAppear:animated]; - } + } } - (void)repositionPopoverFromRect:(CGRect)rect + inView:(UIView *)theView permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections { - [(WEPopoverContainerView *)self.view updatePositionWithAnchorRect:rect - displayArea:[self displayAreaForView:self.view.superview] - permittedArrowDirections:arrowDirections]; - popoverArrowDirection = ((WEPopoverContainerView *)self.view).arrowDirection; + + CGRect displayArea = [self displayAreaForView:theView]; + WEPopoverContainerView *containerView = (WEPopoverContainerView *)self.view; + [containerView updatePositionWithAnchorRect:rect + displayArea:displayArea + permittedArrowDirections:arrowDirections]; + + popoverArrowDirection = containerView.arrowDirection; + containerView.frame = [theView convertRect:containerView.frame toView:backgroundView]; } #pragma mark - @@ -190,6 +207,15 @@ - (void)viewWasTouched:(WETouchableView *)view { @implementation WEPopoverController(Private) +- (UIView *)keyView { + UIWindow *w = [[UIApplication sharedApplication] keyWindow]; + if (w.subviews.count > 0) { + return [w.subviews objectAtIndex:0]; + } else { + return w; + } +} + - (void)setView:(UIView *)v { if (view != v) { [view release]; @@ -197,6 +223,11 @@ - (void)setView:(UIView *)v { } } +- (void)updateBackgroundPassthroughViews { + backgroundView.passthroughViews = passthroughViews; +} + + - (void)dismissPopoverAnimated:(BOOL)animated userInitiated:(BOOL)userInitiated { if (self.view) { [contentViewController viewWillDisappear:animated]; diff --git a/Classes/WEPopoverTableViewController.h b/Classes/WEPopoverTableViewController.h index a2c1b44..c6d2d59 100644 --- a/Classes/WEPopoverTableViewController.h +++ b/Classes/WEPopoverTableViewController.h @@ -9,9 +9,10 @@ #import #import "WEPopoverController.h" -@interface WEPopoverTableViewController : UITableViewController { +@interface WEPopoverTableViewController : UITableViewController { WEPopoverController *popoverController; NSInteger currentPopoverCellIndex; + Class popoverClass; } @property (nonatomic, retain) WEPopoverController *popoverController; diff --git a/Classes/WEPopoverTableViewController.m b/Classes/WEPopoverTableViewController.m index 7cb8b7a..2f05472 100644 --- a/Classes/WEPopoverTableViewController.m +++ b/Classes/WEPopoverTableViewController.m @@ -33,6 +33,8 @@ - (id)initWithStyle:(UITableViewStyle)style { - (void)viewDidLoad { [super viewDidLoad]; + + popoverClass = [WEPopoverController class]; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; @@ -60,13 +62,13 @@ - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } */ -/* + // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations. - return (interfaceOrientation == UIInterfaceOrientationPortrait); + //return interfaceOrientation == UIInterfaceOrientationPortrait; + return YES; } -*/ - (void)viewDidUnload { @@ -165,7 +167,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath UIViewController *contentViewController = [[WEPopoverContentViewController alloc] initWithStyle:UITableViewStylePlain]; CGRect frame = [tableView cellForRowAtIndexPath:indexPath].frame; - self.popoverController = [[[WEPopoverController alloc] initWithContentViewController:contentViewController] autorelease]; + self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease]; self.popoverController.delegate = self; //Uncomment the line below to allow the table view to handle events while the popover is displayed. @@ -190,8 +192,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath - (IBAction)showPopover:(id)sender { if (!self.popoverController) { + UIViewController *contentViewController = [[WEPopoverContentViewController alloc] initWithStyle:UITableViewStylePlain]; - self.popoverController = [[[WEPopoverController alloc] initWithContentViewController:contentViewController] autorelease]; + self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease]; self.popoverController.delegate = self; self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar]; diff --git a/Classes/WETouchableView.m b/Classes/WETouchableView.m index 57cf901..e04fc9d 100644 --- a/Classes/WETouchableView.m +++ b/Classes/WETouchableView.m @@ -34,14 +34,14 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if (hitView == self) { //Test whether any of the passthrough views would handle this touch testHits = YES; - UIWindow *w = [[UIApplication sharedApplication] keyWindow]; - UIView *superHitView = [w hitTest:point withEvent:event]; + UIView *superHitView = [self.superview hitTest:point withEvent:event]; testHits = NO; if ([self isPassthroughView:superHitView]) { hitView = superHitView; } } + return hitView; } } diff --git a/MainWindow.xib b/MainWindow.xib old mode 100755 new mode 100644 index 43af22c..47d8ac9 --- a/MainWindow.xib +++ b/MainWindow.xib @@ -1,14 +1,14 @@ - + 1056 10J869 - 851 + 823 1038.35 461.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 141 + 132 YES @@ -31,35 +31,37 @@ YES IBFilesOwner - IBCocoaTouchFramework + IBIPadFramework IBFirstResponder - IBCocoaTouchFramework + IBIPadFramework - IBCocoaTouchFramework + IBIPadFramework 292 - {320, 480} + {768, 1004} 1 MSAxIDEAA NO NO - - IBCocoaTouchFramework + + 2 + + IBIPadFramework YES - + 1 - IBCocoaTouchFramework + IBIPadFramework NO @@ -68,9 +70,9 @@ NO YES YES - IBCocoaTouchFramework + IBIPadFramework - + YES NO @@ -79,18 +81,18 @@ Item Show Popover - IBCocoaTouchFramework + IBIPadFramework 1 - IBCocoaTouchFramework + IBIPadFramework - + 1 - IBCocoaTouchFramework + IBIPadFramework NO @@ -211,10 +213,13 @@ -2.CustomClassName 10.CustomClassName 10.IBEditorWindowLastContentRect + 10.IBLastUsedUIStatusBarStylesToTargetRuntimesMap 10.IBPluginDependency 12.IBEditorWindowLastContentRect + 12.IBLastUsedUIStatusBarStylesToTargetRuntimesMap 12.IBPluginDependency 16.IBEditorWindowLastContentRect + 16.IBLastUsedUIStatusBarStylesToTargetRuntimesMap 16.IBPluginDependency 18.IBPluginDependency 21.IBPluginDependency @@ -227,10 +232,22 @@ UIResponder WEPopoverTableViewController {{418, 454}, {320, 480}} + + IBCocoaTouchFramework + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin {{525, 346}, {320, 480}} + + IBCocoaTouchFramework + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin - {{931, 428}, {320, 480}} + {{434, -54}, {768, 1024}} + + IBCocoaTouchFramework + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -567,7 +584,7 @@ 0 - IBCocoaTouchFramework + IBIPadFramework com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS @@ -579,6 +596,6 @@ YES WEPopover.xcodeproj 3 - 141 + 132 diff --git a/WEPopover-Info.plist b/WEPopover-Info.plist index 3289444..4c626b4 100755 --- a/WEPopover-Info.plist +++ b/WEPopover-Info.plist @@ -23,7 +23,7 @@ CFBundleVersion 1.0 LSRequiresIPhoneOS - + NSMainNibFile MainWindow diff --git a/WEPopover.xcodeproj/project.pbxproj b/WEPopover.xcodeproj/project.pbxproj index f6432c0..d62571d 100755 --- a/WEPopover.xcodeproj/project.pbxproj +++ b/WEPopover.xcodeproj/project.pbxproj @@ -292,6 +292,7 @@ PREBINDING = NO; "PROVISIONING_PROFILE[sdk=iphoneos*]" = ""; SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; @@ -306,6 +307,7 @@ OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; PREBINDING = NO; SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; };