Uiviewcontroller *contentviewcontroller =[[Uiviewcontroller alloc] init]; ContentViewController.view.backgroundColor=[Uicolor Yellowcolor]; Uipopovercontroller*popcontroller =[[Uipopovercontroller alloc] initwithcontentviewcontroller:contentviewcontroller];//Popcontroller.popoverbackgroundviewclass = [Popoverbackgroundview class];Popcontroller.popovercontentsize = Cgsizemake (300.0f, -); Self.popcontroller=Popcontroller; UIButton* Button =[UIButton Buttonwithtype:uibuttontypecustom]; Button.frame= CGRectMake ( -, -, -, -); Button.backgroundcolor=[Uicolor Redcolor]; [Button addtarget:self action: @selector (Buttonaction) forcontrolevents:uicontroleventtouchupinside]; [Contentviewcontroller.view Addsubview:button]; [Self.popcontroller Presentpopoverfrombarbuttonitem:sender Permittedarrowdirections:uipopove Rarrowdirectionup Animated:yes];
You can also customize the background
#import <UIKit/UIKit.h>@interface popoverbackgroundview:uipopoverbackgroundview @end
#import "PopoverBackgroundView.h"#defineKarrowbase 30.0f#defineKarrowheight 20.0f#defineKborderinset 0.0f@interfacePopoverbackgroundview () @property (nonatomic, strong) Uiimageview*Arrowimageview;-(UIImage *) Drawarrowimage: (cgsize) size;@end@implementationPopoverbackgroundview@synthesizeArrowdirection =_arrowdirection;@synthesizeArrowoffset =_arrowoffset;#pragmaMark-graphics methods-(UIImage *) Drawarrowimage: (cgsize) size{uigraphicsbeginimagecontextwithoptions (size, NO,0); Cgcontextref CTX=Uigraphicsgetcurrentcontext (); [[Uicolor Clearcolor] setfill]; Cgcontextfillrect (CTX, CGRectMake (0.0f,0.0f, Size.width, size.height)); Cgmutablepathref Arrowpath=cgpathcreatemutable (); Cgpathmovetopoint (Arrowpath, NULL, (Size.width/2.0f),0.0f);//Top CenterCgpathaddlinetopoint (Arrowpath, NULL, Size.width, size.height);//Bottom RightCgpathaddlinetopoint (Arrowpath, NULL,0.0f, size.height);//Bottom RightCgpathclosesubpath (Arrowpath); Cgcontextaddpath (CTX, Arrowpath); Cgpathrelease (Arrowpath); Uicolor*fillcolor =[Uicolor Yellowcolor]; Cgcontextsetfillcolorwithcolor (CTX, Fillcolor.cgcolor); Cgcontextdrawpath (CTX, Kcgpathfill); UIImage*image =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnimage; }#pragmaMark-uipopoverbackgroundview overrides-(ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Self.backgroundcolor=[Uicolor Clearcolor]; //todo:update with Border image ViewUiimageview *arrowimageview =[[Uiimageview alloc] Initwithframe:cgrectzero]; Self.arrowimageview=Arrowimageview; [Self addSubview:self.arrowImageView]; } returnSelf ;}+(cgfloat) arrowbase{returnkarrowbase;}+(cgfloat) arrowheight{returnkarrowheight;}+(uiedgeinsets) contentviewinsets{returnUiedgeinsetsmake (Kborderinset, Kborderinset, Kborderinset, Kborderinset);}+(BOOL) wantsdefaultcontentappearance{returnNO;}- (void) layoutsubviews{[Super Layoutsubviews]; //todo:test for Arrow uipopoverarrowdirectionCgsize arrowsize = Cgsizemake ([[Selfclass] Arrowbase], [[Selfclass] arrowheight]); Self.arrowImageView.image=[self drawarrowimage:arrowsize]; Self.arrowImageView.frame= CGRectMake (((self.bounds.size.width-arrowsize.width)-Kborderinset),0.0f, Arrowsize.width, arrowsize.height); }
Use of IOS Uipopoverview