iOS Development Uipopovercontroller

Source: Internet
Author: User

1 , overview

is a common controller in the development of the ipad (not allowed on iphone), unlike other controllers, it inherits directly from NSObject, not inherited from Uiviewcontroller, which occupies only a portion of the screen space to present 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:

The first step : Setting up Content controllers

because Uipopovercontroller directly inherit from NSObject , 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".

There are 3 ways to set up a content controller:

(1) When initializing the Uipopovercontroller, pass in a content controller:

-(ID)initwithcontentviewcontroller:(Uiviewcontroller *) Viewcontroller;

(2) @property (nonatomic, retain) Uiviewcontroller

*Contentviewcontroller;

(3)-(void)Setcontentviewcontroller:

(Uiviewcontroller *) Viewcontroller animated: (BOOL) animated;

The above methods and properties are uipopovercontroller.

Step Two: Set the size of the content

That is, the setting shows how much screen space it occupies.

There are 2 ways to set the size of your content:

(1) @property (nonatomic) cgsize popovercontentsize;

(2)-(void) Setpopovercontentsize: (cgsize) Size animated:

(BOOL) animated;

The above methods and properties are uipopovercontroller.

You can also set popovercontentsize in the Viewdidload method of the Uipopovercontroller Contentviewcontroller (content Controller) to set the size of its display in Uipopovercontroller 。

Step three: Set the display location

That is where the settings come from.

There are 2 ways to set the display location:

(1) Surround a uibarbuttonitem display (arrows specify that Uibarbuttonitem)

-(void) Popoverfrombarbuttonitem: (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)

-(void) Presentpopoverfromrect: (cgrect) rect inview: (UIView *) View permittedarrowdirections: ( Uipopoverarrowdirection) arrowdirections animated: (BOOL) animated;

Rect: Specifies the range of rectangles (positions and dimensions) of the area to which the arrow refers

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?

3 , rect and the View Parameters

4 , Set the display location

If you want the arrows to point to a uiview, there are 2 ways to do it, such as pointing to a button.

Method 1:

[PopOver presentPopoverFromRect:button.bounds Inview:button Permittedarrowdirections:uipopoverarrowdirectiondown Animated:yes];

Method 2:

[PopOver PresentPopoverFromRect:button.frame

InView:button.superview permittedarrowdirections:

Uipopoverarrowdirectiondown Animated:yes];

5 , Common Error

This error is often encountered during the use of PopOver:

-[uipopovercontroller Dealloc] Reached while popover is still visible.

The general meaning of the error is that PopOver was destroyed when it was still visible (called Dealloc).

From the error can draw the conclusion:

(1) PopOver objects are not allowed to be destroyed when PopOver is still visible

(2) Before destroying the PopOver object, make sure the popover disappears (not visible)

For example:

-(Ibaction) Menuclick: (Uibarbuttonitem *) Sender {

Create a content Controller

Uiviewcontroller *VC = [[Uiveiwcontroller alloc] init];

Vc.view.backgroundColor = [Uicolor Redcolor];

Uinavigationcontroller *nav = [[Uinavigationcontroller alloc] INITWITHROOTCONTROLLER:VC];

Create PopOver

Uipopovercontroller *titlepopover = [[Uipopovercontroller alloc] initwithcontentviewcontroller:nav];

You can not set the size, there will be a default size 320x480

Settings display to that location

[Titlepopover Presentpopoverfrombarbuttonitem:sender Permittedarrowdirectionany Animated:YES];

}

The above code will report this error, because in Arc mode, when the last sentence is executed, Titlepopover will be reclaimed by the system, but at this time Titlepopover still appear on the screen, so the above error is reported. The workaround is to create a property:

@property (nonatomic, strong) Uipopovercontroller *titlepopover;

Change the red part of the code above to Self.titlepopover.

6 , setting content size via content controller

The content controller can set its own size to display in PopOver:

Prior to iOS 7:

@property (nonatomic,readwrite) cgsize contentsizeforviewinpopover;

Starting with iOS 7:

@property (nonatomic) cgsize preferredcontentsize;

All of the above properties are Uiviewcontroller

7 , Common Properties

Proxy object:

@property (nonatomic, assign) ID <UIPopoverControllerDelegate> delegate;

is visible:

@property (nonatomic, ReadOnly, getter=ispopovervisible) BOOL popovervisible;

Arrow direction:

@property (nonatomic, readonly) uipopoverarrowdirection popoverarrowdirection;

Turn off PopOver (let popover disappear):

-(void) dismisspopoveranimated: (BOOL) animated;

8 , prevent clicks Uipopovercontroller disappear outside the area

By default:

(1) As long as the Uipopovercontroller display on the screen, Uipopovercontroller all the controls behind the default is not to interact with the user normal

(2) Click the control outside the Uipopovercontroller area, 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:

@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.

7. How to IPhone implemented in PopOver the effect

Uipopovercontroller This class is only used in the ipad, in order to achieve the PopOver effect in the iphone, you have to customize the view, you can refer to:

http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000

http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000

iOS Development Uipopovercontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.