成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

移動開發 iOS
本文主要為各位介紹了iOS SDK的自定義Popover彈出窗口的學習內容,并且附帶了源代碼供大家學習之用,希望對大家有所幫助。

1.設置項目

Step 1

打開Xcode,選擇File > New > Project,創建一個新項目,選擇iOS Single View Application,再點擊Next。

Step 2

填寫一些列表格,項目名稱、組織/公司名稱以及公司標識符。在設備那個下拉菜單中選擇iPad,在這一欄下邊僅選擇Automatic Reference Counting,點擊Next。選擇一個地點存放你的文件,點擊創建。

2. 添加Navigation Controller

Step 1

添加Navigation Controller,這樣就能添加一個按鈕來展示popover。點擊AppDelegate.m,找到 application:didFinishLaunchingWithOptions:方法。添加下述代碼來創建一個 navigation controller,設置為root view controller。

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

Step 2

在導航欄上添加一個“+”的按鈕,然后打開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創建了一個“+”的按鈕,我們將要把它添加至導航欄的右邊,接下來我們會使用選擇器執行showPopover:方法。 

3.展示Popover

Step 1

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

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

Step 2

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

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

Step 3

在@implementation下添加如下代碼來定義這個方法:

  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是可見的,那么會將popover隱藏起來,然后從該方法中直接return。如果 popover不可見,那么我們可以創建一個view controller,讓它展示在popover中。然后創建 popover controller,并設置大小。

4. 測試標準的Popover

我們已經創建一個標準的Popover,創建運行你的項目,點擊“+”按鈕來展現一個基本的Popover。

5. 子類化UIPopoverBackgroundView

Step 1

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

Step 2

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

Step 3

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

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

Step 4

這里有3個類方法需要覆蓋,我們使用這個方法來定義一些值。

  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:方法的條件語句中添加如下代碼:

  1. self.backgroundColor = [UIColor grayColor]; 

6.設置Popover Background View屬性

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

  1. #import "PopoverBackgroundView.h" 

還是在ViewController.m文件中,位于我們在showPopover:方法中創建UIPopoverController的下邊,添加下邊一行代碼,

  1. popController.popoverBackgroundViewClass = [PopoverBackgroundView class]; 

7.測試Popover Background View

創建、運行項目,點擊“+”的按鈕來看下popover,可以看到標準的popover已經被取代。

8.設置陰影和圓角

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

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

9.添加Arrow

Step 1

我們需要創建和管理arrow,我們可以為image view聲明一個屬性,在類擴展中添加如下代碼:

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

現在可以對image view進行實例化,使用如下代碼替代initWithFrame:方法條件語句中的代碼:

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

Step 2

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

 

  1. #define kBorderInset 0.0f 

Step 3

為了畫這個arrow,我們需要聲明一個方法來展現,可以在PopoverBackgroundView.m類擴展中添加下邊這個方法聲明:

  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; 

不用輸入圖片,上述代碼可以自動生成一個arrow。

Step 5

每次popover的background view的子類的bounds 改變時,這個arrow的frame需要重新計算。我們可以通過覆蓋layoutSubviews來達到目的,為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. 測試Popover

源文件:

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

責任編輯:閆佳明 來源: cocoachina
相關推薦

2013-06-27 11:10:01

iOS開發自定義UISlider

2009-12-24 15:22:10

WPF繼承自定義窗口

2013-07-18 16:09:10

自定義iOS狀態欄iOS開發iOS學習

2011-08-02 11:17:13

iOS開發 View

2021-01-20 08:58:39

iOS 14桌面圖標快捷指令

2012-06-01 11:02:33

2017-10-25 14:07:54

APPiOSxcode

2011-08-18 09:44:33

iPhone SDK儀表控件UIDialView

2009-10-30 08:47:57

Windows 7窗口排列

2012-12-24 14:42:48

iOS自定義狀態欄

2015-01-15 16:45:05

iOS源碼自定義畫圖

2015-02-12 15:33:43

微信SDK

2015-02-12 15:38:26

微信SDK

2015-10-12 16:47:13

iOS下拉線條動畫

2016-04-06 11:14:48

iOS相機自定義

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2011-06-23 10:49:13

Qt 自定義信號

2009-07-06 16:59:26

JSP自定義標簽

2022-04-24 15:17:56

鴻蒙操作系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产视频精品区 | 成人不卡 | 亚洲精品久久久一区二区三区 | 国产一区二区精品在线观看 | av电影一区| 成人免费淫片aa视频免费 | a黄毛片 | 久久久久国产一区二区三区 | 欧美一级做性受免费大片免费 | 精品日韩一区 | 久久久久久久一区二区三区 | 国产乱码久久久 | 国产精品久久久久久久久久 | 国产一区在线免费观看视频 | 亚洲高清视频一区二区 | 四虎影院美女 | 午夜综合 | 成人精品鲁一区一区二区 | 成人超碰| 在线观看免费福利 | 久久久99精品免费观看 | 3p视频在线观看 | av免费网站在线观看 | 91亚洲国产成人久久精品网站 | 97av在线| 犬夜叉在线观看 | 成人美女免费网站视频 | 亚洲劲爆av | 一区二区三区四区av | 四虎影院一区二区 | 欧美激情久久久久久 | 欧美日韩亚洲国产 | 久草新在线 | 精国产品一区二区三区四季综 | 香蕉久久久| 欧美精品欧美精品系列 | 成年免费大片黄在线观看一级 | 色婷婷综合网站 | 国产乱码精品一区二区三区忘忧草 | 免费在线播放黄色 | 一区二区三区精品视频 |