iOS_21团购_Popover适应iPad横竖屏切换
终于效果图:

代码片段:
//
// DockItemLocation.m
// 帅哥_团购
//
// Created by beyond on 14-8-13.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "DockItemLocation.h"
// 点击dock上面的locationBtn,弹出的Popover封装的控制器,其上方是搜索栏,下方是tableView
#import "CityLocationController.h" // button上面是图片,以下是文字,这是图片在高度上的比例
#define kImageHeightRatioInBtn 0.5 @interface DockItemLocation()<UIPopoverControllerDelegate>
{
//popover控制器,创建出来之后,show方法显示,因此不能够是局部变量,必须用成员变量记住,否则方法btnClick调用完成就销毁了,还怎样 显示捏? UIPopoverController *_popoverCtrl;
}
@end
@implementation DockItemLocation - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 1.调用父类的方法,设置内部的图片
[self setIcon:@"ic_district.png" selectedIcon:@"ic_district_hl.png"]; // 2.自己主动伸缩
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; // 3.设置默认的文字
[self setTitle:@"定位中" forState:UIControlStateNormal];
self.titleLabel.font = [UIFont systemFontOfSize:16];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; // 4.设置图片属性
self.imageView.contentMode = UIViewContentModeCenter; // 5.监听点击【Location定位】,弹出一个Popover控制器
[self addTarget:self action:@selector(locationBtnOnDockClicked) forControlEvents:UIControlEventTouchDown];
}
return self;
}
// 5.监听点击【Location定位】,弹出一个Popover控制器
- (void)locationBtnOnDockClicked
{
// 禁用,仅仅可点击一次
self.enabled = NO;
// 点击dock上面的locationBtn,弹出的Popover封装的控制器,其上方是搜索栏,下方是tableView
CityLocationController *cityVC = [[CityLocationController alloc] init]; // 唯一一个不是继承自UIViewController的控制器,它继承自NSObject
//popover控制器,创建出来之后,show方法显示,因此不能够是局部变量,必须用成员变量记住,否则方法btnClick调用完成就销毁了,还怎样 显示捏?
_popoverCtrl = [[UIPopoverController alloc] initWithContentViewController:cityVC];
// 设置这个Popover控制器的显示的大小
_popoverCtrl.popoverContentSize = CGSizeMake(320, 480);
// 代理,监听Popover控制器的XX事件
_popoverCtrl.delegate = self;
// 由于其它方法也要显示,_popoverCtrl,所以抽取成自己定义方法
[self showPopoverCtrl]; // 由于屏幕旋转时,弹出的popover的指向的位置就不正确了,所以有必要注冊监听屏幕旋转的通知
// 先移除监听器,保证健壮性
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
// 再加入一个监听器,一旦设备出现UIDeviceOrientationDidChangeNotification,就会调用observer的selector方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidRotated) name:UIDeviceOrientationDidChangeNotification object:nil];
}
// 5-1,由于侦听到屏幕旋转了,也要再次显示_popoverCtrl,所以抽取成自己定义方法
- (void)showPopoverCtrl
{
// 显示到哪里? 假设目标view是self自己,则rect使用bounds,由于bounds的原点才是相对于自己
// 假设目标view是self.superView,则rect使用frame,由于frame的原点才是相对于父控件
[_popoverCtrl presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
// 5-2,再加入一个监听器,一旦设备出现UIDeviceOrientationDidChangeNotification,就会调用observer的selector方法
- (void)screenDidRotated
{
if (_popoverCtrl.popoverVisible) {
// 1. 关闭之前位置上面的_popoverCtrl
[_popoverCtrl dismissPopoverAnimated:NO]; // 2. 0.5秒后创建新的位置上的_popoverCtrl
[self performSelector:@selector(showPopoverCtrl) withObject:nil afterDelay:0.5];
}
}
#pragma mark - popOver代理方法
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
// 消失前,让定位button恢复能够点击状态
self.enabled = YES; // 消失前,即popover被销毁的时候。移除注冊的监听器(通知)
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
} #pragma mark - 销毁时,移除当前对控制器对屏幕的监听,防止野指针
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} #pragma mark - 覆写调整图片和文字在button中的Frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat btnW = contentRect.size.width;
CGFloat imgH = contentRect.size.height * kImageHeightRatioInBtn;
return CGRectMake(0, 0, btnW, imgH);
} - (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat btnW = contentRect.size.width;
CGFloat textH = contentRect.size.height * (1 - kImageHeightRatioInBtn);
// 文字在以下,图片在上面
CGFloat textY = contentRect.size.height - textH;
return CGRectMake(0, textY, btnW, textH);
} @end
iOS_21团购_Popover适应iPad横竖屏切换的更多相关文章
- iPad横竖屏代码适配
你可能非常了解用不同的方式去适配不同尺寸的iPhone屏幕,在适配iPhone屏幕时你需要考虑的只是屏幕大小变化带来的UI元素间隔的变化,但是在iPad上主要针对的是横竖屏下完全不同的UI元素的布局, ...
- JS禁止横竖屏切换,强制横竖屏显示
js判断屏幕横竖屏: function orient() { //alert('gete'); if (window.orientation == 0 || window.orientation == ...
- Android APP 简单高效的禁用横竖屏切换
默认情况下,Android APP的界面会随着手机方向的改变而改变,当手机处于竖屏状态,APP的界面也处于竖屏状态,而当手机处于横屏状态,APP也会自动切换到横屏状态.一般情况下APP的界面都是为竖屏 ...
- Android开发之Activity横竖屏切换生命周期重建问题
当进行横竖屏切换的时候Activity的生命周期会重建,从而导致Activity崩溃等问题,为了避免这一问题,需要在AndroidManifest.xml文件中设置: <activity and ...
- Activity 横竖屏切换
前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...
- Android应用:横竖屏切换总结
眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...
- Android横竖屏切换
ps:虽然现在的app一般都是固定一个屏幕方向,但是还是有必要了解下屏幕切换的方法和注意. 一 固定横竖屏 androidmainfest.xml中设置activoty属性:android:scree ...
- Android横竖屏切换及其对应布局加载问题
第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...
- Android横竖屏切换重载问题与小结
(转自:http://www.cnblogs.com/franksunny/p/3714442.html) (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com ...
随机推荐
- JavaScript使用技巧精萃
(一).确认删除用法: 1. BtnDel.Attributes.Add("onclick","return confirm('"+"确认删除?& ...
- EasyUI中combobox的使用方法和一个代码实例
一.easyUI中select下拉框动态添加option选项 问题:想在combobox的下拉项里动态添加一些内容,但是添加不成功.因为jquery easyui的下拉列表combobox是用DIV模 ...
- idea unicode自动转码设置
idea unicode自动转码设置 File > Settings > Editor > File Encodings 右侧 Properties Files 中 选中 Trans ...
- telnet 退出命令
telnet xxx port ctrl + ] telnet > quit ctrl + w 是清除命令 转自: http://wangyifeng.blog.51cto.com/214490 ...
- Package.json小结
生成package.json 定位到想放置package.json的目录,运行npm init,根据提示就可以生成package.json文件,其中test command可以为空. 安装mo ...
- IMA文件如何打开,winimage使用方
一般先用UltraISO打开一个系统的镜像文件(.iso).其中有些文件(尤其是.ima,img)比如下面雨林木风Ghost系统盘的这个IMA文件,我们先提取到桌面 用WinImage打开这个文件即可 ...
- java发送email(含代理方式,ssl方式,传统方式)
package spring.vhostall.com; import java.security.Security; import java.util.Date; import java.util. ...
- 关于NHibernate中存在于Session中实例的3种状态的简单分析
在使用NHibernate的时候.在Session中会有3种状态. 1. 瞬时状态 (Transient) 由 new 命令开辟内存空间的对象,也就是平时所熟悉的普通对象. 如: Student st ...
- 【VMware】宿主机连接wifi,虚拟机中的Linux系统配置连接wifi
环境描述 宿主机:Windows 10 64bit 虚拟机:Centos 第一步:虚拟机设置 选择连接方式为NAT 第二步:设置宿主机的wifi 控制面板>>网络和Internet> ...
- 类的专有方法(__getattr__和__setattr__、__delattr__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.360doc.com/content/15/0413/19/12067640_4629 ...