Skip to content

Commit

Permalink
Fixed support for interface rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
werner77 committed May 7, 2011
1 parent 1207820 commit 2bd05b5
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 57 deletions.
1 change: 1 addition & 0 deletions Classes/UIBarButtonItem+WEPopover.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
@interface UIBarButtonItem(WEPopover)

- (CGRect)frameInView:(UIView *)v;
- (UIView *)superview;

@end
45 changes: 31 additions & 14 deletions Classes/UIBarButtonItem+WEPopover.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion Classes/WEPopoverController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
CGSize popoverContentSize;
WEPopoverContainerViewProperties *containerViewProperties;
id <NSObject> context;
NSArray *passthroughViews;
NSArray *passthroughViews;
}

@property(nonatomic, retain) UIViewController *contentViewController;
Expand Down Expand Up @@ -61,6 +61,7 @@
animated:(BOOL)animated;

- (void)repositionPopoverFromRect:(CGRect)rect
inView:(UIView *)view
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections;

@end
57 changes: 44 additions & 13 deletions Classes/WEPopoverController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

@interface WEPopoverController(Private)

- (UIView *)keyView;
- (void)updateBackgroundPassthroughViews;
- (void)setView:(UIView *)v;
- (CGRect)displayAreaForView:(UIView *)theView;
- (WEPopoverContainerViewProperties *)defaultContainerViewProperties;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -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 -
Expand All @@ -190,13 +207,27 @@ - (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];
view = [v retain];
}
}

- (void)updateBackgroundPassthroughViews {
backgroundView.passthroughViews = passthroughViews;
}


- (void)dismissPopoverAnimated:(BOOL)animated userInitiated:(BOOL)userInitiated {
if (self.view) {
[contentViewController viewWillDisappear:animated];
Expand Down
3 changes: 2 additions & 1 deletion Classes/WEPopoverTableViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#import <UIKit/UIKit.h>
#import "WEPopoverController.h"

@interface WEPopoverTableViewController : UITableViewController<WEPopoverControllerDelegate> {
@interface WEPopoverTableViewController : UITableViewController<WEPopoverControllerDelegate, UIPopoverControllerDelegate> {
WEPopoverController *popoverController;
NSInteger currentPopoverCellIndex;
Class popoverClass;
}

@property (nonatomic, retain) WEPopoverController *popoverController;
Expand Down
13 changes: 8 additions & 5 deletions Classes/WEPopoverTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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];

Expand Down
4 changes: 2 additions & 2 deletions Classes/WETouchableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 2bd05b5

Please sign in to comment.