自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS SDK:自定義Popover(彈出窗口)

移動(dòng)開發(fā) iOS
本文主要為各位介紹了iOS SDK的自定義Popover彈出窗口的學(xué)習(xí)內(nèi)容,并且附帶了源代碼供大家學(xué)習(xí)之用,希望對(duì)大家有所幫助。

1.設(shè)置項(xiàng)目

Step 1

打開Xcode,選擇File > New > Project,創(chuàng)建一個(gè)新項(xiàng)目,選擇iOS Single View Application,再點(diǎn)擊Next。

Step 2

填寫一些列表格,項(xiàng)目名稱、組織/公司名稱以及公司標(biāo)識(shí)符。在設(shè)備那個(gè)下拉菜單中選擇iPad,在這一欄下邊僅選擇Automatic Reference Counting,點(diǎn)擊Next。選擇一個(gè)地點(diǎn)存放你的文件,點(diǎn)擊創(chuàng)建。

2. 添加Navigation Controller

Step 1

添加Navigation Controller,這樣就能添加一個(gè)按鈕來(lái)展示popover。點(diǎn)擊AppDelegate.m,找到 application:didFinishLaunchingWithOptions:方法。添加下述代碼來(lái)創(chuàng)建一個(gè) navigation controller,設(shè)置為root view controller。

  1. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
  2. self.window.rootViewController = navController; 

Step 2

在導(dǎo)航欄上添加一個(gè)“+”的按鈕,然后打開ViewController.m文件,在[super viewDidLoad]下邊把如下代碼添加至viewDidLoad方法中。

  1. UIBarButtonItem *popoverButton = [[UIBarButtonItem alloc] 
  2. initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
  3.                     target:self 
  4.                     action:@selector(showPopover:)]; 
  5. self.navigationItem.rightBarButtonItem = popoverButton; 

UIBarButtonSystemItemAdd創(chuàng)建了一個(gè)“+”的按鈕,我們將要把它添加至導(dǎo)航欄的右邊,接下來(lái)我們會(huì)使用選擇器執(zhí)行showPopover:方法。 

3.展示Popover

Step 1

在執(zhí)行showPopover:方法前先為popover controller添加一個(gè)屬性,打開ViewController.h文件,添加如下屬性:

  1. @property (nonatomic, strong) UIPopoverController *popController; 

Step 2

回到ViewController.m文件,在類擴(kuò)展中聲明showPopover:方法,如下:

  1. @interface ViewController () 
  2. - (void)showPopover:(id)sender; 
  3. @end 

Step 3

在@implementation下添加如下代碼來(lái)定義這個(gè)方法:

  1. - (void)showPopover:(id)sender 
  2.    if (self.popController.popoverVisible) { 
  3.        [self.popController dismissPopoverAnimated:YES]; 
  4.        return
  5.    } 
  6.     UIViewController *contentViewController = [[UIViewController alloc] init]; 
  7.     contentViewController.view.backgroundColor = [UIColor yellowColor]; 
  8.     UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController]; 
  9.     popController.popoverContentSize = CGSizeMake(300.0f, 600.0f); 
  10.     self.popController = popController; 
  11.     [self.popController presentPopoverFromBarButtonItem:sender 
  12. permittedArrowDirections:UIPopoverArrowDirectionUp 
  13.                                     animated:YES]; 

首先檢查下popover能否展示在屏幕上。如果popover是可見的,那么會(huì)將popover隱藏起來(lái),然后從該方法中直接return。如果 popover不可見,那么我們可以創(chuàng)建一個(gè)view controller,讓它展示在popover中。然后創(chuàng)建 popover controller,并設(shè)置大小。

4. 測(cè)試標(biāo)準(zhǔn)的Popover

我們已經(jīng)創(chuàng)建一個(gè)標(biāo)準(zhǔn)的Popover,創(chuàng)建運(yùn)行你的項(xiàng)目,點(diǎn)擊“+”按鈕來(lái)展現(xiàn)一個(gè)基本的Popover。

5. 子類化UIPopoverBackgroundView

Step 1

為了自定義popover,我們需要子類化UIPopoverBackgroundView。點(diǎn)擊 File > New > File, 選擇iOS Cocoa Touch Objective-C Class, 點(diǎn)擊Next.

Step 2

給class這一欄填上PopoverBackgroundView,從Subclass of下拉菜單中選擇UIPopoverBackgroundView。

Step 3

這里有兩個(gè)UIPopoverBackgroundView屬性需要被覆蓋,添加如下代碼來(lái)定義arrow的方向和位移。

  1. @synthesize arrowDirection  = _arrowDirection; 
  2. @synthesize arrowOffset     = _arrowOffset; 

Step 4

這里有3個(gè)類方法需要覆蓋,我們使用這個(gè)方法來(lái)定義一些值。

  1. #define kArrowBase 30.0f 
  2. #define kArrowHeight 20.0f 
  3. #define kBorderInset 8.0f 

Step 5

添加如下代碼覆蓋arrowBase, arrowHeight和contentViewInsets方法。

  1. + (CGFloat)arrowBase 
  2.     return kArrowBase; 
  3. + (CGFloat)arrowHeight 
  4.     return kArrowHeight; 
  5. + (UIEdgeInsets)contentViewInsets 
  6.     return UIEdgeInsetsMake(kBorderInset, kBorderInset, kBorderInset,       kBorderInset); 

arrowBase方法確定arrow底部的寬度,arrowHeight方法確定arrow的高度。

Step 6

添加背景色,在initWithFrame:方法的條件語(yǔ)句中添加如下代碼:

  1. self.backgroundColor = [UIColor grayColor]; 

6.設(shè)置Popover Background View屬性

測(cè)試popover之前,我們需要輸入和設(shè)置popover controller的 popover Background View Class Property。打開ViewController.m文件,輸入 popover background view頭文件:

  1. #import "PopoverBackgroundView.h" 

還是在ViewController.m文件中,位于我們?cè)趕howPopover:方法中創(chuàng)建UIPopoverController的下邊,添加下邊一行代碼,

  1. popController.popoverBackgroundViewClass = [PopoverBackgroundView class]; 

7.測(cè)試Popover Background View

創(chuàng)建、運(yùn)行項(xiàng)目,點(diǎn)擊“+”的按鈕來(lái)看下popover,可以看到標(biāo)準(zhǔn)的popover已經(jīng)被取代。

8.設(shè)置陰影和圓角

wantsDefaultContentAppearance 方法決定是否在popover中展示默認(rèn)的內(nèi)置陰影和圓角,如果返回的是“NO”,Popover Background View將不再展示默認(rèn)的陰影 和圓角,允許執(zhí)行你自己的。添加如下代碼來(lái)覆蓋之前的方法:

  1. + (BOOL)wantsDefaultContentAppearance 
  2. return NO; 

9.添加Arrow

Step 1

我們需要?jiǎng)?chuàng)建和管理arrow,我們可以為image view聲明一個(gè)屬性,在類擴(kuò)展中添加如下代碼:

  1. @property (nonatomic, strong) UIImageView *arrowImageView; 

現(xiàn)在可以對(duì)image view進(jìn)行實(shí)例化,使用如下代碼替代initWithFrame:方法條件語(yǔ)句中的代碼:

  1. self.backgroundColor = [UIColor clearColor]; 
  2. UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 
  3. self.arrowImageView = arrowImageView; 
  4. [self addSubview:self.arrowImageView]; 

Step 2

通過使用以下代碼來(lái)更新在PopoverBackgroundView.m定義的kBorderInset來(lái)改變border inset:

 

  1. #define kBorderInset 0.0f 

Step 3

為了畫這個(gè)arrow,我們需要聲明一個(gè)方法來(lái)展現(xiàn),可以在PopoverBackgroundView.m類擴(kuò)展中添加下邊這個(gè)方法聲明:

  1. - (UIImage *)drawArrowImage:(CGSize)size; 

Step 4

在@implementation下添加方法定義:

  1. - (UIImage *)drawArrowImage:(CGSize)size 
  2.     UIGraphicsBeginImageContextWithOptions(size, NO, 0); 
  3.     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
  4.     [[UIColor clearColor] setFill]; 
  5.     CGContextFillRect(ctx, CGRectMake(0.0f, 0.0f, size.width, size.height)); 
  6.     CGMutablePathRef arrowPath = CGPathCreateMutable(); 
  7.     CGPathMoveToPoint(arrowPath, NULL, (size.width/2.0f), 0.0f); 
  8.     CGPathAddLineToPoint(arrowPath, NULL, size.width, size.height); 
  9.     CGPathAddLineToPoint(arrowPath, NULL, 0.0f, size.height); 
  10.     CGPathCloseSubpath(arrowPath); 
  11.     CGContextAddPath(ctx, arrowPath); 
  12.     CGPathRelease(arrowPath); 
  13.     UIColor *fillColor = [UIColor yellowColor]; 
  14.    CGContextSetFillColorWithColor(ctx, fillColor.CGColor); 
  15.     CGContextDrawPath(ctx, kCGPathFill); 
  16.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
  17.     UIGraphicsEndImageContext(); 
  18.     return image; 

不用輸入圖片,上述代碼可以自動(dòng)生成一個(gè)arrow。

Step 5

每次popover的background view的子類的bounds 改變時(shí),這個(gè)arrow的frame需要重新計(jì)算。我們可以通過覆蓋layoutSubviews來(lái)達(dá)到目的,為layoutSubviews添加如下代碼:

  1. - (void)layoutSubviews 
  2.     [super layoutSubviews]; 
  3.     CGSize arrowSize = CGSizeMake([[self class] arrowBase], [[self class] arrowHeight]); 
  4.     self.arrowImageView.image = [self drawArrowImage:arrowSize]; 
  5.     self.arrowImageView.frame = CGRectMake(((self.bounds.size.width - arrowSize.width) kBorderInset), 0.0f, arrowSize.width, arrowSize.height); 

10. 測(cè)試Popover

源文件:

http://down.51cto.com/data/816045

責(zé)任編輯:閆佳明 來(lái)源: cocoachina
相關(guān)推薦

2013-06-27 11:10:01

iOS開發(fā)自定義UISlider

2009-12-24 15:22:10

WPF繼承自定義窗口

2013-07-18 16:09:10

自定義iOS狀態(tài)欄iOS開發(fā)iOS學(xué)習(xí)

2012-06-01 11:02:33

2017-10-25 14:07:54

APPiOSxcode

2021-01-20 08:58:39

iOS 14桌面圖標(biāo)快捷指令

2011-08-02 11:17:13

iOS開發(fā) View

2011-08-18 09:44:33

iPhone SDK儀表控件UIDialView

2009-10-30 08:47:57

Windows 7窗口排列

2015-02-12 15:33:43

微信SDK

2015-01-15 16:45:05

iOS源碼自定義畫圖

2012-12-24 14:42:48

iOS自定義狀態(tài)欄

2015-02-12 15:38:26

微信SDK

2015-10-12 16:47:13

iOS下拉線條動(dòng)畫

2016-04-06 11:14:48

iOS相機(jī)自定義

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2011-06-23 10:49:13

Qt 自定義信號(hào)

2009-07-06 16:59:26

JSP自定義標(biāo)簽

2011-12-16 14:23:51

Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)