First, a brief introduction
1. What is Uipopovercontroller
is a common controller in ipad development (not allowed on the iphone)
Unlike other controllers, it inherits directly from NSObject and does not inherit from Uiviewcontroller
It takes up only a portion of the screen space to render information and is displayed at the front of the screen
2. Use steps
To display a uipopovercontroller, you need to go through the following steps
(1) Set up content controller
Because Uipopovercontroller directly inherits from the NSObject, does not have the visualization ability. Therefore, the contents of the Uipopovercontroller must be provided by another controller that inherits from Uiviewcontroller, which is called the "content controller"
(2) Set the size of the content
Show how much screen space it occupies
(3) show where it pops up
Second, the specific steps
code example:
Create an ipad project and write the following code:
Create a new controller that inherits from UITableView as a popovercontroller content controller.
YYMENUVIEWCONTROLLER.M file
Copy Code code as follows:
//
Yymenuviewcontroller.m
01-popovercontroller Simple Introduction
//
Created by Apple on 14-8-17.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYMenuViewController.h"
@interface Yymenuviewcontroller ()
@property (Nonatomic,strong) Nsarray *menus;
@end
Copy Code code as follows:
@implementation Yymenuviewcontroller
-(Nsarray *) menus
{
if (_menus==nil) {
_menus=@[@ "List 1", @ "List 2", @ "List 3", @ "List 4"];
}
return _menus;
}
-(void) viewdidload
{
[Super Viewdidload];
}
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return self.menus.count;
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
Static NSString *id=@ "ID";
UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id];
if (Cell==nil) {
Cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];
}
Cell.textlabel.text=self.menus[indexpath.row];
return cell;
}
@end
Copy Code code as follows:
YYVIEWCONTROLLER.M file
//
Yyviewcontroller.m
01-popovercontroller Simple Introduction
//
Created by Apple on 14-8-17.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYViewController.h"
#import "YYMenuViewController.h"
@interface Yyviewcontroller ()
@property (Nonatomic,strong) Uipopovercontroller *popover;
@end
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
}
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
1. Create a new content controller
Yymenuviewcontroller *menuvc=[[yymenuviewcontroller Alloc]init];
2. Create a new Popovercontroller and set its content controller
Self.popover=[[uipopovercontroller ALLOC]INITWITHCONTENTVIEWCONTROLLER:MENUVC];
3. Set the size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Show
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
}
@end
The effect of the implementation of the following figure:
Description: A navigation controller has been added to the storyboard and two buttons have been added.
Third, common error
This error is often encountered during the use of PopOver
Copy Code code as follows:
-[uipopovercontroller Dealloc] Reached while popover is still visible.
The general meaning of the error is: PopOver was destroyed when it was still visible (called Dealloc)
The conclusion that can be drawn from the error
When PopOver is still visible, do not destroy PopOver objects.
Make sure that popover disappears (invisible) before destroying the PopOver object
For example: In the above code, if the global variable popover is not applicable, the above error will occur.
Four, set the size
hint: Do not suggest, like below this bar PopOver's width and height is written dead.
Copy Code code as follows:
1. Create a new content controller
Yymenuviewcontroller *menuvc=[[yymenuviewcontroller Alloc]init];
2. Create a new Popovercontroller and set its content controller
Self.popover=[[uipopovercontroller ALLOC]INITWITHCONTENTVIEWCONTROLLER:MENUVC];
3. Set the size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Show
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
A better design is that the size of the popover should be determined by the contents of the internal controller.
The content controller can set itself the size that it displays in PopOver, and there are two ways:
(1) before iOS 7 @property (nonatomic,readwrite) cgsize contentsizeforviewinpopover;
(2) starting from iOS 7 @property (nonatomic) cgsize preferredcontentsize;
All of the above attributes are Uiviewcontroller
Copy Code code as follows:
-(Nsarray *) menus
{
if (_menus==nil) {
_menus=@[@ "List 1", @ "List 2", @ "List 3", @ "List 4", @ "List 4", @ "List 4", @ "List 4", @ "List 4", @ "List 4", @ "List 4", @ "List 1", @ "List 2", @ "List 1", @ "" Listing 2 ";
}
return _menus;
}
-(void) viewdidload
{
[Super Viewdidload];
Setting the controller's future size in PopOver
CGFloat maxh=min (480,self.menus.count*44);
iOS7 Previous Settings
Self.contentsizeforviewinpopover=cgsizemake (MAXH);
After iOS7
Self.preferredcontentsize=cgsizemake (MAXH);
}
Effect:
With respect to min (a,b), the final size depends on B, but the maximum cannot exceed a, and the value equals a if more than a is exceeded.
Five, set the location of the display
1. There are 2 ways to set the display location
(1) around a uibarbuttonitem display (arrows specify that Uibarbuttonitem)
Copy Code code as follows:
-(void) Presentpopoverfrombarbuttonitem: (Uibarbuttonitem *) Item Permittedarrowdirections: (uipopoverarrowdirection ) arrowdirections animated: (BOOL) animated;
Item: Which uibarbuttonitem to display around
Arrowdirections: Direction of the Arrow
Animated: Whether it is displayed by animation
(2) display around a particular area (the arrow specifies that particular area)
Copy Code code as follows:
-(void) Presentpopoverfromrect: (cgrect) rect inview: (UIView *) View permittedarrowdirections: ( Uipopoverarrowdirection) arrowdirections animated: (BOOL) animated;
Rect: Specifies the rectangular frame range (position and size) of the area to which the arrow refers, with the upper-left corner of the view as the coordinate origin
The View:rect parameter is the coordinate origin in the upper-left corner of the view (0,0).
Arrowdirections: Direction of the Arrow
Animated: Whether it is displayed by animation
The rect and view parameters are as follows:
Related code:
Copy Code code as follows:
//
Yyviewcontroller.m
01-popovercontroller Simple Introduction
//
Created by Apple on 14-8-17.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYViewController.h"
#import "YYMenuViewController.h"
@interface Yyviewcontroller () <UIPopoverControllerDelegate>
@property (Nonatomic,strong) Uipopovercontroller *popover;
-(Ibaction) ButtonClick: (UIButton *) sender;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
}
-(void) Showpopoverfromitem
{
1. Create a new content controller
Yymenuviewcontroller *menuvc=[[yymenuviewcontroller Alloc]init];
2. Create a new Popovercontroller and set its content controller
Self.popover=[[uipopovercontroller ALLOC]INITWITHCONTENTVIEWCONTROLLER:MENUVC];
3. Set the size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Show
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
5. Set up Agent
self.popover.delegate=self;
}
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
}
#pragma mark-Proxy method
Call when Popovercontroller disappears.
-(void) Popovercontrollerdiddismisspopover: (Uipopovercontroller *) Popovercontroller
{
}
Call when the position of the popovercontroller is changed (e.g. vertical screen variable horizontal screen)
-(void) Popovercontroller: (Uipopovercontroller *) Popovercontroller willrepositionpopovertorect: (inout CGRect *) rect InView: (inout uiview *__autoreleasing *) view
{
}
Used to determine if the user clicked on the mask, Popovercontroller can be dismiss, return yes to the can, return no representative can not
-(BOOL) Popovercontrollershoulddismisspopover: (Uipopovercontroller *) Popovercontroller
{
return NO;
}
-(Ibaction) ButtonClick: (UIButton *) Sender {
1. Create a new Popovercontroller and set its content controller
Yymenuviewcontroller *menuvc=[[yymenuviewcontroller Alloc]init];
Self.popover=[[uipopovercontroller ALLOC]INITWITHCONTENTVIEWCONTROLLER:MENUVC];
2. Show
2.1 The first way
[Self.popover presentpopoverfrombarbuttonitem:<# (Uibarbuttonitem *) #> permittedarrowdirections:<# ( uipopoverarrowdirection) #> animated:<# (BOOL) #>];
2.2 Second Way
[Self.popover presentPopoverFromRect:sender.bounds Inview:sender permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
Description: PopOver will point to sender.bounds this rectangular box, which takes the upper-left corner of the sender as the coordinate origin.
Note: Note the difference between Sender.frame and sender.bounds
}
@end
Interface effect: (partial)
A diagram of the calculation of the frame coordinates:
The following two are equivalent:
That is, if you want the arrow to point to a uiview, there are 2 ways to do it, such as pointing to a button
Method 1
Copy Code code as follows:
[PopOver presentPopoverFromRect:button.bounds Inview:button Permittedarrowdirections:uipopoverarrowdirectiondown Animated:yes];
Method 2
Copy Code code as follows:
[PopOver presentPopoverFromRect:button.frame InView:button.superview permittedarrowdirections: Uipopoverarrowdirectiondown Animated:yes];
VI. Set up Agent
Proxy Object
Copy Code code as follows:
@property (nonatomic, assign) ID <UIPopoverControllerDelegate> delegate;
is visible
Copy Code code as follows:
@property (nonatomic, ReadOnly, getter=ispopovervisible) BOOL popovervisible;
Arrow direction
Copy Code code as follows:
@property (nonatomic, readonly) uipopoverarrowdirection popoverarrowdirection;
Close PopOver (let popover disappear)
Copy Code code as follows:
-(void) dismisspopoveranimated: (BOOL) animated;
Code Description:
Copy Code code as follows:
.......
5. Set up Agent
self.popover.delegate=self;
}
#pragma mark-Proxy method
Call when Popovercontroller disappears.
-(void) Popovercontrollerdiddismisspopover: (Uipopovercontroller *) Popovercontroller
{
}
Call when the position of the popovercontroller is changed (e.g. vertical screen variable horizontal screen)
-(void) Popovercontroller: (Uipopovercontroller *) Popovercontroller willrepositionpopovertorect: (inout CGRect *) rect InView: (inout uiview *__autoreleasing *) view
{
}
Used to determine if the user clicked on the mask, Popovercontroller can be dismiss, return yes to the can, return no representative can not
-(BOOL) Popovercontrollershoulddismisspopover: (Uipopovercontroller *) Popovercontroller
{
return NO;
}
Seven, prevent the click Uipopovercontroller disappear outside the area
By default
As long as the Uipopovercontroller is displayed on the screen, all the controls behind Uipopovercontroller are not able to interact properly with the user by default
Click the control outside the Uipopovercontroller area, Uipopovercontroller the default will disappear
To click a control outside the Uipopovercontroller area to not let the uipopovercontroller disappear, the solution is to set the Passthroughviews property
Copy Code code as follows:
@property (nonatomic, copy) Nsarray *passthroughviews;
This property is the setting of which controls can continue to interact normally with the user when Uipopovercontroller is displayed. In this case, clicking on a control outside the area doesn't make uipopovercontroller disappear.
code example:
Copy Code code as follows:
-(Ibaction) ButtonClick: (UIButton *) Sender {
1. Create a new Popovercontroller and set its content controller
Yymenuviewcontroller *menuvc=[[yymenuviewcontroller Alloc]init];
Self.popover=[[uipopovercontroller ALLOC]INITWITHCONTENTVIEWCONTROLLER:MENUVC];
Settings filter out some controls
Self.popover.passthroughviews=@[self.switchview];
2. Show
2.1 The first way
[Self.popover presentpopoverfrombarbuttonitem:<# (Uibarbuttonitem *) #> permittedarrowdirections:<# ( uipopoverarrowdirection) #> animated:<# (BOOL) #>];
2.2 Second Way
[Self.popover presentPopoverFromRect:sender.bounds Inview:sender permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
[Self.popover presentPopoverFromRect:sender.frame InView:sender.superview permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
Description: PopOver will point to sender.bounds this rectangular box, which takes the upper-left corner of the sender as the coordinate origin.
Note: Note the difference between Sender.frame and sender.bounds
}
Add:
Uipopovercontroller This class is only for use in the ipad
To achieve the PopOver effect in the iphone, you have to customize view, you can refer to