diff --git a/FPPopoverController.h b/FPPopoverController.h index f8aee39..68a76c7 100644 --- a/FPPopoverController.h +++ b/FPPopoverController.h @@ -67,6 +67,10 @@ /** @brief Presenting the popover from a specified point **/ -(void)presentPopoverFromPoint:(CGPoint)fromPoint; +/** @brief PResenting the popover from uibarbuttonitem **/ +- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)barButtonItem; + + /** @brief Dismiss the popover **/ -(void)dismissPopoverAnimated:(BOOL)animated; diff --git a/FPPopoverController.m b/FPPopoverController.m index 28406d4..58a8edd 100644 --- a/FPPopoverController.m +++ b/FPPopoverController.m @@ -8,6 +8,7 @@ #import "FPPopoverController.h" +#import "UIBarButtonItem+FPPopover.h" //ivars @interface FPPopoverController() @@ -144,6 +145,8 @@ -(id)initWithViewController:(UIViewController*)viewController [_touchView addSubview:_contentView]; [_contentView addContentView:_viewController.view]; + [self addChildViewController:_viewController]; + _viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.view.clipsToBounds = NO; @@ -243,9 +246,10 @@ -(void)presentPopoverFromPoint:(CGPoint)fromPoint //keep the first subview if(_window.subviews.count > 0) { + [_viewController beginAppearanceTransition:YES animated:NO]; _parentView = [_window.subviews objectAtIndex:0]; [_parentView addSubview:self.view]; - [_viewController viewDidAppear:YES]; + [_viewController endAppearanceTransition]; } } @@ -305,6 +309,12 @@ -(CGPoint)originFromView:(UIView*)fromView return p; } +- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)barButtonItem { + UIView *lastView = [self lastView]; + CGRect rect = [barButtonItem frameInView:lastView]; + [self presentPopoverFromPoint:CGPointMake(rect.origin.x + rect.size.width/2, rect.size.height + rect.origin.y)]; +} + -(void)presentPopoverFromView:(UIView*)fromView { SAFE_ARC_RELEASE(_fromView); @@ -603,6 +613,17 @@ -(void)setAlpha:(CGFloat)alpha self.view.alpha = alpha; } +#pragma mark - View Helpers + +- (UIView*)lastView { + UIWindow *w = [[UIApplication sharedApplication] keyWindow]; + if (w.subviews.count > 0) { + return [w.subviews objectAtIndex:0]; + } else { + return w; + } +} + diff --git a/FPPopoverDemo.xcodeproj/project.pbxproj b/FPPopoverDemo.xcodeproj/project.pbxproj index 2f2b7e0..958547f 100644 --- a/FPPopoverDemo.xcodeproj/project.pbxproj +++ b/FPPopoverDemo.xcodeproj/project.pbxproj @@ -46,6 +46,7 @@ 18EF6DEB154ED27D005AB1DD /* background.png in Resources */ = {isa = PBXBuildFile; fileRef = 18EF6DE9154ED27D005AB1DD /* background.png */; }; 18EF6DEC154ED27D005AB1DD /* background@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 18EF6DEA154ED27D005AB1DD /* background@2x.png */; }; 18F357271584E6E700AD4F07 /* FPDemoTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F357261584E6E700AD4F07 /* FPDemoTableViewController.m */; }; + EDF3CDC517C517C7000331D4 /* UIBarButtonItem+FPPopover.m in Sources */ = {isa = PBXBuildFile; fileRef = EDF3CDC417C517C7000331D4 /* UIBarButtonItem+FPPopover.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -105,6 +106,8 @@ 18EF6DEA154ED27D005AB1DD /* background@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "background@2x.png"; sourceTree = ""; }; 18F357251584E6E700AD4F07 /* FPDemoTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FPDemoTableViewController.h; sourceTree = ""; }; 18F357261584E6E700AD4F07 /* FPDemoTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FPDemoTableViewController.m; sourceTree = ""; }; + EDF3CDC317C517C6000331D4 /* UIBarButtonItem+FPPopover.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIBarButtonItem+FPPopover.h"; sourceTree = ""; }; + EDF3CDC417C517C7000331D4 /* UIBarButtonItem+FPPopover.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIBarButtonItem+FPPopover.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -237,6 +240,8 @@ 186C43931538512A00502D64 /* FPPopover */ = { isa = PBXGroup; children = ( + EDF3CDC317C517C6000331D4 /* UIBarButtonItem+FPPopover.h */, + EDF3CDC417C517C7000331D4 /* UIBarButtonItem+FPPopover.m */, 18E1272F16D97A6500FB6CD0 /* ARCMacros.h */, 186C43941538513B00502D64 /* FPPopoverController.h */, 186C43951538513B00502D64 /* FPPopoverController.m */, @@ -447,6 +452,7 @@ 186C43A115386E2200502D64 /* DemoTableController.m in Sources */, 18F357271584E6E700AD4F07 /* FPDemoTableViewController.m in Sources */, 18C427041764920D001D4A24 /* FPPopoverKeyboardResponsiveController.m in Sources */, + EDF3CDC517C517C7000331D4 /* UIBarButtonItem+FPPopover.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FPPopoverDemo/DemoTableController.m b/FPPopoverDemo/DemoTableController.m index bd478a0..b7bee3f 100644 --- a/FPPopoverDemo/DemoTableController.m +++ b/FPPopoverDemo/DemoTableController.m @@ -23,6 +23,14 @@ - (void)viewDidLoad self.title = @"Popover Title"; } +- (void)viewWillAppear:(BOOL)animated { + +} + +- (void)viewDidAppear:(BOOL)animated { + +} + - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { diff --git a/FPPopoverDemo/FPViewController.m b/FPPopoverDemo/FPViewController.m index 8bbad1a..a2ef15f 100644 --- a/FPPopoverDemo/FPViewController.m +++ b/FPPopoverDemo/FPViewController.m @@ -11,6 +11,7 @@ #import "FPPopoverController.h" #import "FPDemoTableViewController.h" +#import "UIBarButtonItem+FPPopover.h" @interface FPViewController () @@ -24,7 +25,12 @@ @implementation FPViewController - (void)viewDidLoad { [super viewDidLoad]; - [self.navigationController setNavigationBarHidden:YES]; + [self.navigationController setNavigationBarHidden:NO]; + UIBarButtonItem *barButtonSample = [[UIBarButtonItem alloc]initWithTitle:@"BarButtonSample" + style:UIBarButtonItemStyleBordered + target:self + action:@selector(popover:)]; + self.navigationItem.rightBarButtonItem = barButtonSample; //KEYBOARD OBSERVERS /************************/ @@ -93,6 +99,9 @@ -(IBAction)popover:(id)sender popover.arrowDirection = FPPopoverNoArrow; [popover presentPopoverFromPoint: CGPointMake(self.view.center.x, self.view.center.y - popover.contentSize.height/2)]; } + else if ([sender isKindOfClass:[UIBarButtonItem class]]) { + [popover presentPopoverFromBarButtonItem:sender]; + } else { //sender is the UIButton view popover.arrowDirection = FPPopoverArrowDirectionAny; diff --git a/FPPopoverDemo/en.lproj/FPViewController_iPhone.xib b/FPPopoverDemo/en.lproj/FPViewController_iPhone.xib index d6198de..deff874 100644 --- a/FPPopoverDemo/en.lproj/FPViewController_iPhone.xib +++ b/FPPopoverDemo/en.lproj/FPViewController_iPhone.xib @@ -2,9 +2,9 @@ 1552 - 12D78 + 12E55 3084 - 1187.37 + 1187.39 626.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -42,7 +42,6 @@ 274 {320, 460} - _NS:9 NO @@ -57,7 +56,6 @@ 292 {{20, 20}, {71, 31}} - _NS:9 NO @@ -93,7 +91,6 @@ 293 {{123, 20}, {75, 31}} - _NS:9 NO @@ -116,7 +113,6 @@ 289 {{225, 20}, {75, 31}} - _NS:9 NO @@ -139,7 +135,6 @@ 257 {{222, 214}, {82, 31}} - _NS:9 NO @@ -162,7 +157,6 @@ 256 {{119, 214}, {82, 31}} - _NS:9 NO @@ -185,7 +179,6 @@ 260 {{20, 214}, {82, 31}} - _NS:9 NO @@ -208,7 +201,6 @@ 268 {{20, 409}, {82, 31}} - _NS:9 NO @@ -231,7 +223,6 @@ 264 {{114, 409}, {92, 31}} - _NS:9 NO @@ -254,8 +245,6 @@ 265 {{217, 409}, {92, 31}} - - _NS:9 NO IBCocoaTouchFramework @@ -277,7 +266,6 @@ 289 {{278, 114}, {44, 37}} - _NS:9 NO @@ -307,7 +295,6 @@ 292 {{0, 114}, {44, 37}} - _NS:9 NO @@ -330,7 +317,6 @@ 288 {{123, 114}, {74, 37}} - _NS:9 NO @@ -353,7 +339,6 @@ 292 {{198, 284}, {92, 44}} - _NS:9 NO @@ -376,7 +361,6 @@ 292 {{21, 284}, {123, 44}} - _NS:9 NO @@ -399,7 +383,6 @@ 288 {{50, 66}, {221, 30}} - _NS:9 NO @@ -435,7 +418,6 @@ {{0, 20}, {320, 460}} - 1 @@ -752,111 +734,7 @@ 48 - - - - FPViewController - UIViewController - - id - id - id - id - id - id - id - id - id - id - id - id - id - id - id - - - - bottomCenter: - id - - - bottomLeft: - id - - - bottomRight: - id - - - goToTableView: - id - - - lt: - id - - - midCenter: - id - - - midLeft: - id - - - midRight: - id - - - navControllerPopover: - id - - - noArrow: - id - - - popover: - id - - - rt: - id - - - topCenter: - id - - - topLeft: - id - - - topRight: - id - - - - UIButton - UIButton - - - - noArrow - UIButton - - - transparentPopover - UIButton - - - - IBProjectSource - ./Classes/FPViewController.h - - - - + 0 IBCocoaTouchFramework diff --git a/UIBarButtonItem+FPPopover.h b/UIBarButtonItem+FPPopover.h new file mode 100644 index 0000000..313b444 --- /dev/null +++ b/UIBarButtonItem+FPPopover.h @@ -0,0 +1,15 @@ +// +// UIBarButtonItem+FPPopover.h +// FPPopoverDemo +// +// Created by Kamil Badyla on 21.08.2013. +// Copyright (c) 2013 Fifty Pixels Ltd. All rights reserved. +// + +#import + +@interface UIBarButtonItem (FPPopover) + +- (CGRect)frameInView:(UIView *)v; + +@end diff --git a/UIBarButtonItem+FPPopover.m b/UIBarButtonItem+FPPopover.m new file mode 100644 index 0000000..7718d17 --- /dev/null +++ b/UIBarButtonItem+FPPopover.m @@ -0,0 +1,29 @@ +// +// UIBarButtonItem+FPPopover.m +// FPPopoverDemo +// +// Created by Kamil Badyla on 21.08.2013. +// Copyright (c) 2013 Fifty Pixels Ltd. All rights reserved. +// + +#import "UIBarButtonItem+FPPopover.h" + +@implementation UIBarButtonItem (FPPopover) + +- (CGRect)frameInView:(UIView *)v { + + UIView *mainView = self.customView; + if (!mainView && [self respondsToSelector:@selector(view)]) { + mainView = [self performSelector:@selector(view)]; + } + NSUInteger indexOfView = [mainView.superview.subviews indexOfObject:mainView]; + if (mainView.superview.subviews.count > 0 && indexOfView != NSNotFound) { + UIView *button = [mainView.superview.subviews objectAtIndex:indexOfView]; + return [button convertRect:button.bounds toView:v]; + } + else { + return CGRectZero; + } +} + +@end