iphone 与 iPad在开发中的区别以及iPad中的UIPopoverController的使用
2015-11-04 18:39
507 查看
共同点:用的都是IOS系统
不同点:
键盘:
iPad特有的API
UIPopoverController(左图)
UISplitViewController(右图)
屏幕方向的支持
设备支持的应用程序
iPhone上只能运行iPhone程序
iPad上能够运行iPhone\iPad程序
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UIPopoverController
是iPad开发中常见的一种控制器(在iPhone上不允许使用)
跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController
它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面
使用步骤
要想显示一个UIPopoverController,需要经过下列步骤
设置内容控制器
由于UIPopoverController直接继承自NSObject,不具备可视化的能力
因此UIPopoverController上面的内容必须由另外一个继承自UIViewController的控制器来提供,这个控制器称为“内容控制器”
设置内容的尺寸
显示出来占据多少屏幕空间
设置显示的位置
从哪个地方冒出来
设置内容控制器
设置内容控制器有3种方法
在初始化UIPopoverController的时候传入一个内容控制器
- (id)initWithContentViewController:(UIViewController*)viewController;
// 如果不想使用初始化的内容控制器使用这个属性可以再次指定内容控制器
@property (nonatomic,
retain) UIViewController *contentViewController;
- (void)setContentViewController:(UIViewController*)viewController animated:(BOOL)animated;
设置内容的尺寸
设置内容的尺寸有2种方法
@property (nonatomic)
CGSize popoverContentSize;
- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
设置显示的位置
设置显示的位置有2种方法
1、围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;
item :围绕着哪个UIBarButtonItem显示
arrowDirections :箭头的方向
animated:是否通过动画显示出来
2、围绕着某一块特定区域显示(箭头指定那块特定区域)
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView*)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;
rect :指定箭头所指区域的矩形框范围(位置和尺寸)
view :rect参数是以view的左上角为坐标原点(0,0)
arrowDirections :箭头的方向
animated:是否通过动画显示出来
图示:
常用属性
代理对象
@property (nonatomic,
assign) id <UIPopoverControllerDelegate>delegate;
是否可见
@property (nonatomic,
readonly, getter=isPopoverVisible)BOOLpopoverVisible;
箭头方向
@property (nonatomic,
readonly) UIPopoverArrowDirection popoverArrowDirection;
关闭popover(让popover消失)
- (void)dismissPopoverAnimated:(BOOL)animated;
默认情况下
只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的
点击UIPopoverController区域外的控件,UIPopoverController默认会消失
要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性
@property (nonatomic,
copy) NSArray *passthroughViews;
这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
示例:1、指向导航控制器的左边按钮
2、指向任意控件
不同点:
键盘:
iPad特有的API
UIPopoverController(左图)
UISplitViewController(右图)
屏幕方向的支持
设备支持的应用程序
iPhone上只能运行iPhone程序
iPad上能够运行iPhone\iPad程序
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UIPopoverController
是iPad开发中常见的一种控制器(在iPhone上不允许使用)
跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController
它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面
使用步骤
要想显示一个UIPopoverController,需要经过下列步骤
设置内容控制器
由于UIPopoverController直接继承自NSObject,不具备可视化的能力
因此UIPopoverController上面的内容必须由另外一个继承自UIViewController的控制器来提供,这个控制器称为“内容控制器”
设置内容的尺寸
显示出来占据多少屏幕空间
设置显示的位置
从哪个地方冒出来
设置内容控制器
设置内容控制器有3种方法
在初始化UIPopoverController的时候传入一个内容控制器
- (id)initWithContentViewController:(UIViewController*)viewController;
// 如果不想使用初始化的内容控制器使用这个属性可以再次指定内容控制器
@property (nonatomic,
retain) UIViewController *contentViewController;
- (void)setContentViewController:(UIViewController*)viewController animated:(BOOL)animated;
设置内容的尺寸
设置内容的尺寸有2种方法
@property (nonatomic)
CGSize popoverContentSize;
- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
设置显示的位置
设置显示的位置有2种方法
1、围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;
item :围绕着哪个UIBarButtonItem显示
arrowDirections :箭头的方向
animated:是否通过动画显示出来
2、围绕着某一块特定区域显示(箭头指定那块特定区域)
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView*)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;
rect :指定箭头所指区域的矩形框范围(位置和尺寸)
view :rect参数是以view的左上角为坐标原点(0,0)
arrowDirections :箭头的方向
animated:是否通过动画显示出来
图示:
常用属性
代理对象
@property (nonatomic,
assign) id <UIPopoverControllerDelegate>delegate;
是否可见
@property (nonatomic,
readonly, getter=isPopoverVisible)BOOLpopoverVisible;
箭头方向
@property (nonatomic,
readonly) UIPopoverArrowDirection popoverArrowDirection;
关闭popover(让popover消失)
- (void)dismissPopoverAnimated:(BOOL)animated;
默认情况下
只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的
点击UIPopoverController区域外的控件,UIPopoverController默认会消失
要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性
@property (nonatomic,
copy) NSArray *passthroughViews;
这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
示例:1、指向导航控制器的左边按钮
// 弹出菜单的单击事件 - (IBAction)popMenu:(id)item { // 0.内容 HMMenuViewController *menu = [[HMMenuViewController alloc] init]; // 1.创建一个UIPopover UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]]; /** UIPopoverArrowDirectionUp = 1UL << 0, 指向上面 UIPopoverArrowDirectionDown = 1UL << 1, 指向下面 UIPopoverArrowDirectionLeft = 1UL << 2, 指向左边 UIPopoverArrowDirectionRight = 1UL << 3,指向右边 UIPopoverArrowDirectionAny = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown | UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight, 指向系统认为好的方向 UIPopoverArrowDirectionUnknown = NSUIntegerMax */ // 3.从哪里显示出来 --> 指向item [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; // 不允许popover还在显示的时候挂掉 // popover必须在消失的时候挂掉 (ios8以后就不需要了) self.popover = popover; }HMMenuViewController.m文件
#import "HMMenuViewController.h" @interface HMMenuViewController () @property (nonatomic, strong) NSArray *titles; @end @implementation HMMenuViewController - (void)viewDidLoad { [super viewDidLoad]; self.titles = @[@"设置", @"清除缓存", @"退出", @"基本信息"]; self.title = @"菜单"; self.view.backgroundColor = [UIColor blueColor]; CGFloat w = 320; CGFloat h = self.titles.count * 44; self.preferredContentSize = CGSizeMake(w, h); // self.contentSizeForViewInPopover = CGSizeMake(w, h); } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.titles.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } cell.textLabel.text = self.titles[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIViewController *vc = [[UIViewController alloc] init]; vc.view.backgroundColor = [UIColor redColor]; [self.navigationController pushViewController:vc animated:YES]; }
2、指向任意控件
- (IBAction)buttonClick:(UIButton *)btn { // 0.内容 HMColorsViewController *colors = [[HMColorsViewController alloc] init]; colors.delegate = self; // 1.创建 self.popover = [[UIPopoverController alloc] initWithContentViewController:colors]; self.popover.delegate = self; // 2.显示 [self.popover presentPopoverFromRect:btn.frame inView:btn.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; // 3.设置哪里控件在popover显示出来的时候,仍旧可以跟用户进行交互 self.popover.passthroughViews = @[self.greenButton]; self.colorButton = btn; } #pragma mark - 颜色选择代理 - (void)colorsViewController:(HMColorsViewController *)vc didSelectColor:(UIColor *)color { self.colorButton.backgroundColor = color; // 关闭popover [self.popover dismissPopoverAnimated:YES]; } #pragma mark - popover的代理 - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { // NSLog(@"popover销毁了"); }
#import <UIKit/UIKit.h> @class HMColorsViewController; @protocol HMColorsViewControllerDelegate <NSObject> @optional - (void)colorsViewController:(HMColorsViewController *)vc didSelectColor:(UIColor *)color; @end @interface HMColorsViewController : UITableViewController @property (nonatomic, weak) id<HMColorsViewControllerDelegate> delegate; @endHMColorsViewController.m
- (void)viewDidLoad { [super viewDidLoad]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 30; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(colorsViewController:didSelectColor:)]) { UIColor *color = [tableView cellForRowAtIndexPath:indexPath].backgroundColor; [self.delegate colorsViewController:self didSelectColor:color]; } }
相关文章推荐
- iOS9 UIPopoverViewController
- Archive 创建报错问题
- iOS_通过UIBezierPath实现简单的画板功能
- iOS-UI控件精讲之UILabel
- UISegmentedControll,UISlider,UISwitch,UIStepper
- 写给程序员和UI--Android的切图标准
- ios开发系列-UITableController-动态页面
- JPA 学习篇(四)Query接口下的 API 测试
- UILabel 和UIButton的简单实用
- uiCoder界面代码生成工具 part1 概述+图片制作
- The JSP specification requires that an attribute name is preceded by whitespace异常
- IQueryable接口与IEnumberable 区别
- Android设计模式——Builder模式
- TrueCrypt的安装和使用
- UIImage部分拉伸——stretchableImageWithLeftCapWidth的使用
- Longest Increasing Subsequence的两种解法
- UITableViewStyleGrouped和plain的两种格式的区别
- Ext中关于Ext.QuickTips.init()的使用
- media query 代码实例
- does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), o