QQ空间HD(2)-UIPopoverController其它使用
DJTestViewController.m
#import "DJTestViewController.h"
#import "DJColorTableViewController.h" @interface DJTestViewController() <DJColorControllerDelegate> @property (nonatomic,weak) UIButton *btn1;
@property (nonatomic,weak) UIButton *btn2;
@property (nonatomic,strong) UIPopoverController *popVC; @end @implementation DJTestViewController - (void)viewDidLoad { [super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; UIButton *btn1 = [[UIButton alloc] init];
btn1.frame = CGRectMake(, , , );
[btn1 setTitle:@"测试" forState:UIControlStateNormal];
[btn1 setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:btn1];
self.btn1 = btn1; [btn1 addTarget:self action:@selector(btn1DidClick:) forControlEvents:UIControlEventTouchUpInside]; UIButton *btn2 = [[UIButton alloc] init];
btn2.backgroundColor = [UIColor redColor];
btn2.frame = CGRectMake(, , , );
[btn2 setTitle:@"测试2" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(btn2DidClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2];
self.btn2 = btn2; } - (void)btn1DidClick:(UIButton *)btn { DJColorTableViewController *colorVC = [[DJColorTableViewController alloc] init];
colorVC.delegate = self; UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:colorVC]; // 使popover在弹出时btn2仍然可以被点击
popVC.passthroughViews = @[self.btn2]; [popVC presentPopoverFromRect:CGRectMake(, , , ) inView:[self.btn1 superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; self.popVC = popVC; } - (void)btn2DidClick:(UIButton *)btn {
NSLog(@"btn2 被点击"); } // 当popover 里面的CELL被点击时调用此方法
- (void)colorTableViewController:(DJColorTableViewController *)vc didSelectedColor:(UIColor *)color { self.btn1.backgroundColor = color; [self.popVC dismissPopoverAnimated:YES]; } @end
DJColorTableViewController.h
#import <UIKit/UIKit.h> @class DJColorTableViewController;
@protocol DJColorControllerDelegate <NSObject> @optional
- (void)colorTableViewController:(DJColorTableViewController *)vc didSelectedColor:(UIColor *)color; @end @interface DJColorTableViewController : UITableViewController @property (nonatomic,weak) id<DJColorControllerDelegate> delegate; @end
DJColorTableViewController.m
#import "DJColorTableViewController.h"
#define DJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
static NSUInteger numberOfROws = ;
@implementation DJColorTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 取消tableview cell分隔线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 内部决定将来显示在popover里面的宽度和高度
self.preferredContentSize = CGSizeMake(, * numberOfROws);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfROws;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"color";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.backgroundColor = DJRandomColor;
// 取消cell选中状态
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIColor *backgroundColor = [tableView cellForRowAtIndexPath:indexPath].backgroundColor;
if ([self.delegate respondsToSelector:@selector(colorTableViewController:didSelectedColor:)]) {
[self.delegate colorTableViewController:self didSelectedColor:backgroundColor];
}
}
@end
UIPopoverController 需要掌握的其它要点:
1. 使popover里面的controller可以自行决定其宽高:
在本例中popover里面的controller就是DJColorTableViewController
self.preferredContentSize = CGSizeMake(, * numberOfROws);
2. 让popover里面的发生的事件可以告诉外面(代理、通知)
3. 使popover消失
[self.popVC dismissPopoverAnimated:YES];
4. 在popover的蒙版弹出时,蒙版后面的按钮仍然可以被点击
// 使popover在弹出时btn2仍然可以被点击
popVC.passthroughViews = @[self.btn2];
5. 使popover可以在任意View的位置被弹出,不仅仅是UIBarButtonItem
[popVC presentPopoverFromRect:CGRectMake(, , , ) inView:[self.btn1 superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
方法说明:popover会在指定View的指定位置弹出。
第一个参数frame代表指定的位置。
第二个参数View代表相对于哪个View。

当然了,如果想在btn1 view的下方显示,最简便的写法如下:
[popVC presentPopoverFromRect:self.btn1.bounds inView:self.btn1 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
苹果这样设计可以方便popover从任何位置弹出。
QQ空间HD(2)-UIPopoverController其它使用的更多相关文章
- QQ空间HD(1)-UIPopoverController基本使用
UIPopoverController 是iPad的专属API ViewController.m #import "ViewController.h" #import " ...
- QQ空间HD(6)-实现自定义的选项卡切换效果
DJTabbarButton.m #import "DJTabbarButton.h" @implementation DJTabbarButton - (instancetype ...
- QQ空间HD(5)-添加左侧菜单栏内容
DJIconView.m #import "DJIconView.h" @implementation DJIconView - (instancetype)initWithFra ...
- QQ空间HD(4)-设置左侧菜单栏属性
DJHomeViewController.m #import "DJHomeViewController.h" #import "DJMenuView.h" ; ...
- QQ空间HD(3)-Modal的切换效果总结
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UIViewController ...
- iOS开发UI篇—模仿ipad版QQ空间登录界面
iOS开发UI篇—模仿ipad版QQ空间登录界面 一.实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分 3.登陆界面的 ...
- QQ空间动态爬虫
作者:虚静 链接:https://zhuanlan.zhihu.com/p/24656161 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 先说明几件事: 题目的意 ...
- 仿QQ空间根据位置弹出PopupWindow显示更多操作效果
我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: ...
- QQ空间/朋友圈类界面的搭建
类似于QQ空间的布局主要是在说说信息.点赞.回复三大部分的自适应布局上. 当我们需要搭建类似QQ空间.微信朋友圈的界面的时候,可做如下操作: 创建一个对应的model类: 创建一个对应model类的f ...
随机推荐
- Numpy 用法小结
1. asarray 函数 可以将输入数据转化为矩阵格式. 输入数据可以是(列表,元组,列表的列表,元组的元组,元组的列表等这些数组形式). >>> asarray([(1,2,3 ...
- Leetcode 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 【poj3254】 Corn Fields
http://poj.org/problem?id=3254 (题目链接) 题意 给出一块n*m的田地,有些能够耕种,有些不能.要求将牛两两不相邻的放在田中,牛的个数至少为1个.问有多少种放法. So ...
- Jenkins配置MSBuild时使用环境变量
[MSBuild Plugin]插件在使用环境变量有个很奇葩的方式,比如我们通常在Windows的节点机器上,使用WORKSPACE环境变量时,批处理应该这样写%WORKSPACE%,而有时插件确不能 ...
- Linux列出安装过的程序
命令行: dpkg -l apt-cache(模糊搜索apt-cache search 包名) pkgnames yum list(ubuntu下试了无效) rpm -aq(ubuntu下试了无效)
- joomla \libraries\joomla\session\session.php 反序列化截断畸形字符串导致对象注入漏洞
catalog . 漏洞描述 . PHP SESSION持久化 . PHP 序列化/反序列化内核实现 . 漏洞代码分析 . POC构造技巧 . 防御方案 . Code Pathc方案 1. 漏洞描述 ...
- ELF Executable Reconstruction From A Core Image
catalog . INTRODUCTION . THE PROCESS IMAGE . THE CORE IMAGE . EXECUTABLE RECONSTRUCTION . FAILURES I ...
- U盘容量减少的解决办法
今天是使用以前的U盘的时候发现原来4G的U盘容量居然只剩下了700M,不是说u盘的可用空间是700M,而是在电脑上面显示的总空间为700M.在电脑上面格式化之后也没起作用. 经过Google找到了在w ...
- 数据结构作业——brothers(二叉树)
brothers Description 给你一棵节点编号从 1 到 n 的,根节点为 1 的二叉树.然后有 q 个询问,每个询问给出一个整数表示树的节点,要求这个节点的兄弟节点数目和堂兄弟节点的数目 ...
- Ubuntu学习总结-08 Ubuntu运行Shell脚本报 shell /bin/bash^M: bad interpreter错误问题解决
错误原因之一很有可能是运行的脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, 其ASCII码分别是0x0D, 0x0A.可以有很多种办法看这个文件是DOS格式的还是UNIX格式的, 还是MA ...