This article mainly introduces the use of Uipopovercontroller in iOS development, code based on the traditional objective-c, the need for friends can refer to the following
First, Brief introduction
1. What is Uipopovercontroller
is a common controller in ipad development (not allowed on 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 present information and is displayed at the front of the screen
2. Steps to use
To display a uipopovercontroller, you need to go through the following steps
(1) Setting up the content controller
Since Uipopovercontroller directly inherits from NSObject, it does not have the ability to visualize. So the uipopovercontroller above must be provided by another controller that inherits from the Uiviewcontroller, which is called the "content controller".
(2) Setting the size of the content
Show how much screen space it occupies
(3) show where it pops up
Ii. Specific steps
code example:
To create a new ipad project, write the following code:
Create a new controller that inherits from UITableView, making it a content controller for Popovercontroller.
YYMENUVIEWCONTROLLER.M file
Copy CodeThe code is as follows:
//
Yymenuviewcontroller.m
01-popovercontroller Brief 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 CodeThe code is as follows:
@implementation Yymenuviewcontroller
-(Nsarray *) menus
{
if (_menus==nil) {
[ email protected][@ "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 *[email protected] "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 CodeThe code is as follows:
YYVIEWCONTROLLER.M file
//
Yyviewcontroller.m
01-popovercontroller Brief 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. Setting the Size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Display
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
}
@end
Implementation results such as:
Description: Added a navigation controller to the storyboard and added two buttons.
Three, common error
This error is often encountered during the use of PopOver.
Copy CodeThe code is as follows:
-[uipopovercontroller Dealloc] Reached while popover is still visible.
The general meaning of the error is that PopOver is destroyed when it is still visible (called Dealloc)
A conclusion that can be drawn from a mistake
When PopOver is still visible, popover objects are not allowed to be destroyed.
Before destroying PopOver objects, be sure to let popover disappear (not visible)
For example: In the above code, if the global variable popover is not applicable, then the above error will occur.
Four, set the size
Tip: Not recommended, like the following bar popover width and height write dead.
Copy CodeThe code is 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. Setting the Size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Display
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
A better design is: the size of the popover should be determined by the contents of the internal controller.
The content controller can set its own dimensions for display in PopOver, which are available in two ways:
(1) before iOS 7 @property (nonatomic,readwrite) cgsize contentsizeforviewinpopover;
(2) starting from iOS 7 @property (nonatomic) cgsize preferredcontentsize;
All of the above properties are Uiviewcontroller
Copy CodeThe code is as follows:
-(Nsarray *) menus
{
if (_menus==nil) {
[Email protected] [@ ' 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" @ "List 2"];
}
return _menus;
}
-(void) viewdidload
{
[Super Viewdidload];
Setting the controller's future dimensions 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, a, b), the final size depends on a, but the maximum cannot exceed a, and if it exceeds a then the value equals a.
Five, set the location of the display
1. There are 2 ways to set the display location
(1) Surround a uibarbuttonitem display (arrows specify that Uibarbuttonitem)
Copy CodeThe code is as follows:
-(void) Presentpopoverfrombarbuttonitem: (Uibarbuttonitem *) Item Permittedarrowdirections: (uipopoverarrowdirection ) arrowdirections animated: (BOOL) animated;
Item: Which Uibarbuttonitem is displayed around
Arrowdirections: Direction of Arrow
Animated: Is it animated?
(2) display around a specific area (arrows specify that specific area)
Copy CodeThe code is as follows:
-(void) Presentpopoverfromrect: (cgrect) rect inview: (UIView *) View permittedarrowdirections: ( Uipopoverarrowdirection) arrowdirections animated: (BOOL) animated;
Rect: Specifies the bounding rectangle (position and size) of the area to which the arrow refers, with the upper-left corner of the view as the origin of the coordinates
The View:rect parameter is the coordinate origin (0,0) in the upper-left corner of the view
Arrowdirections: Direction of Arrow
Animated: Is it animated?
The rect and view parameters are as follows:
Related code:
Copy CodeThe code is as follows:
//
Yyviewcontroller.m
01-popovercontroller Brief 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 CodeThe code is 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. Setting the Size
Self.popover.popovercontentsize=cgsizemake (300, 200);
4. Display
[Self.popover PresentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
5. Setting up the agent
self.popover.delegate=self;
}
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
}
#pragma mark-Proxy method
Call when Popovercontroller disappears.
-(void) Popovercontrollerdiddismisspopover: (Uipopovercontroller *) Popovercontroller
{
}
Popovercontroller when the position of the change is changed (such as vertical screen to change the 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 dismiss, return yes to represent can, return no represents not can
-(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. Display
2.1 The first one way
[Self.popover presentpopoverfrombarbuttonitem:<# (Uibarbuttonitem *) #> permittedarrowdirections:<# ( uipopoverarrowdirection) #> animated:<# (BOOL) #>];
2.2 Second Way
[Self.popover presentPopoverFromRect:sender.bounds Inview:sender permittedarrowdirections: Uipopoverarrowdirectionany Animated:yes];
Note: PopOver will point to sender.bounds, a rectangular box with the upper-left corner of sender as the origin of the coordinates.
Note: Note the difference between Sender.frame and sender.bounds
}
@end
Interface effect: (partial)
A diagram of the frame coordinate calculation:
The following two are equivalent:
That is, if you want the arrow to point to a uiview, there are 2 ways, such as pointing to a button
Method 1
Copy CodeThe code is as follows:
[PopOver presentPopoverFromRect:button.bounds Inview:button Permittedarrowdirections:uipopoverarrowdirectiondown Animated:yes];
Method 2
Copy CodeThe code is as follows:
[PopOver presentPopoverFromRect:button.frame InView:button.superview permittedarrowdirections: Uipopoverarrowdirectiondown Animated:yes];
Vi. setting up an agent
Proxy Object
Copy CodeThe code is as follows:
@property (nonatomic, assign) ID <UIPopoverControllerDelegate> delegate;
is visible
Copy CodeThe code is as follows:
@property (nonatomic, ReadOnly, getter=ispopovervisible) BOOL popovervisible;
Arrow direction
Copy CodeThe code is as follows:
@property (nonatomic, readonly) uipopoverarrowdirection popoverarrowdirection;
Close PopOver (let popover disappear)
Copy CodeThe code is as follows:
-(void) dismisspopoveranimated: (BOOL) animated;
Code Description:
Copy CodeThe code is as follows:
.......
5. Setting up the agent
self.popover.delegate=self;
}
#pragma mark-Proxy method
Call when Popovercontroller disappears.
-(void) Popovercontrollerdiddismisspopover: (Uipopovercontroller *) Popovercontroller
{
}
Popovercontroller when the position of the change is changed (such as vertical screen to change the 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 dismiss, return yes to represent can, return no represents not can
-(BOOL) Popovercontrollershoulddismisspopover: (Uipopovercontroller *) Popovercontroller
{
return NO;
}
Seven, prevent the click Uipopovercontroller area disappears outside
By default
As long as the Uipopovercontroller is displayed on the screen, all the controls behind Uipopovercontroller are not allowed to interact properly with the user by default
Click Controls outside the Uipopovercontroller area, and Uipopovercontroller will disappear by default
To not let Uipopovercontroller disappear when you click a control outside the Uipopovercontroller area, the workaround is to set the Passthroughviews property
Copy CodeThe code is as follows:
@property (nonatomic, copy) Nsarray *passthroughviews;
This property is set to what controls can continue to interact normally with the user when Uipopovercontroller is displayed. In this case, clicking on a control outside the area won't let the uipopovercontroller disappear.
code example:
Copy CodeThe code is 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 to filter out some controls
[Email protected] [Self.switchview];
2. Display
2.1 The first one 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];
Note: PopOver will point to sender.bounds, a rectangular box with the upper-left corner of sender as the origin of the coordinates.
Note: Note the difference between Sender.frame and sender.bounds
}
Add:
Uipopovercontroller This class is only available on ipad
To achieve the PopOver effect in the iphone, you have to customize the view to see
Articles you may be interested in:
- Introduction to how to use the Uiswitch button in iOS development
- Uiactivityviewcontroller of iOS9 system share call
- An example of creating a basic animation for iOS app UI development
Use of Uipopovercontroller in iOS development