版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、ios疯狂详解之自定义状态栏代码解析_ 这里说明一下,apple没有开放的状态栏的api,在ios 的官方文档没有提到修改window level的方式; 先看一下window level的可用的值包括: 1: typedef cgfloat uiwindowlevel; 2: const uiwindowlevel uiwindowlevelnormal; / 0.0 3: const uiwindowlevel uiwindowlevelalert; / 2021.0 4: const uiwindowlevel uiwindowlevelstatusbar; / 1000.0 默认我们的
2、uiview layer都是在uiwindowlevelnormal上,这也就是为什么系统弹出来的对话框在我们的视图之上,由于它的window level级别更高。 依据windowlevel的原理我们也就知道,假如想在系统的状态栏上,添加自定义的状态栏,就需要比uiwindowlevelstatusbar的级别更高,接下来,用代码说明一下: 首先,先建一个single view application,名字自定义就可以了, 然后,新建一个类命名为: statusbaroverlay 继承自uiwindow类,代码: statusbaroverlay.h文件 1: #import 2: 3:
3、interface statusbaroverlay : uiwindow 4: uiview *contentview; 5: uilabel *textlabel; 6: 7: 8: property (nonatomic, retain) uiview *contentview; 9: 10: property (nonatomic, retain) uilabel *textlabel; 11: 12: end statusbaroverlay.m文件 1: / 2: / statusbaroverlay.m 3: / statusbardemo 4: / 5: / created b
4、y jordy wang on 12-8-7. 6: / copyright (c) 2021年 _mycompanyname_. all rights reserved. 7: / 8: 9: #import statusbaroverlay.h 10: 11: #define status_bar_orientation uiapplication sharedapplication.statusbarorientation 12: #define rotation_animation_duration uiapplication sharedapplication.statusbaror
5、ientationanimationduration 13: 14: 15: interface statusbaroverlay() 16: 17: - (void)initializetodefaultstate; 18: - (void)rotatestatusbarwithframe:(nsvalue *)framevalue; 19: - (void)setsubviewhframe; 20: - (void)setsubviewvframe; 21: end 22: 23: 24: implementation statusbaroverlay 25: synthesize con
6、tentview; 26: synthesize textlabel; 27: 28: /重写init方法 29: - (id)init 30: 31: self = super initwithframe:cgrectzero; 32: if (self) 33: self.windowlevel = uiwindowlevelstatusbar + 1; 34: self.frame = uiapplication sharedapplication.statusbarframe; 35: self setbackgroundcolor:uicolor orangecolor; 36: s
7、elf sethidden:no; 37: 38: /内容视图 39: uiview *_contentview = uiview alloc initwithframe:self.bounds; 40: self.contentview = _contentview; 41: self.contentview setautoresizingmask:uiviewautoresizingflexiblewidth; 42: self.contentview setbackgroundcolor:uicolor cyancolor; 43: self addsubview:self.conten
8、tview; 44: _contentview release; 45: 46: 47: /添加textlabel 48: uilabel *_textlabel = uilabel alloc initwithframe:cgrectmake(30, 0, cgrectgetwidth(self.frame)-60, cgrectgetheight(self.frame); 49: self.textlabel = _textlabel; 50: self.textlabel setbackgroundcolor:uicolor bluecolor; 51: self.textlabel s
9、etfont:uifont systemfontofsize:12; 52: self.textlabel settextalignment:uitextalignmentcenter; 53: self.textlabel settextcolor:uicolor blackcolor; 54: self.textlabel settext:自定义的状态栏 author by jordy; 55: self.contentview addsubview:self.textlabel; 56: _textlabel release; 57: 58: /注册监听-当屏幕将要转动时,所动身的大事(
10、用于操作本视图转变其frame) 59: nsnotificationcenter defaultcenter addobserver:self selector:selector(willrotatescreenevent:) name:uiapplicationwillchangestatusbarframenotification object:nil; 60: /初始化 61: self initializetodefaultstate; 62: 63: 64: return self; 65: 66: 67: 68: 69: 70: /初始化为默认状态 71: - (void)ini
11、tializetodefaultstate 72: 73: /猎取当前的状态栏位置 74: cgrect statusbarframe = uiapplication sharedapplication.statusbarframe; 75: /设置当前视图的旋转, 依据当前设备的朝向 76: self rotatestatusbarwithframe:nsvalue valuewithcgrect:statusbarframe; 77: 78: 79: 80: 81: 82: 83: /旋转屏幕 84: - (void)rotatestatusbarwithframe:(nsvalue *)
12、framevalue 85: 86: cgrect frame = framevalue cgrectvalue; 87: uiinterfaceorientation orientation = status_bar_orientation; 88: 89: if (orientation = uideviceorientationportrait) 90: self.transform = cgaffinetransformidentity; /屏幕不旋转 91: self setsubviewvframe; 92: else if (orientation = uideviceorien
13、tationportraitupsidedown) 93: self.transform = cgaffinetransformmakerotation(m_pi); /屏幕旋转180度 94: self setsubviewvframe; 95: else if (orientation = uideviceorientationlandscaperight) 96: self.transform = cgaffinetransformmakerotation(m_pi * (-90.0f) / 180.0f); /屏幕旋转-90度 97: self setsubviewhframe; 98
14、: else if (orientation = uideviceorientationlandscapeleft) 99: self.transform = cgaffinetransformmakerotation(m_pi * 90.0f / 180.0f); /屏幕旋转90度 100: self setsubviewhframe; 101: 102: 103: self.frame = frame; 104: self.contentview setframe:self.bounds; 105: 106: 107: /设置横屏的子视图的frame 108: - (void)setsub
15、viewhframe 109: 110: self.textlabel.frame = cgrectmake(30, 0, 1024-60, 20); 111: 112: /设置竖屏的子视图的frame 113: - (void)setsubviewvframe 114: 115: self.textlabel.frame = cgrectmake(30, 0, 748-60, 20); 116: 117: 118: #pragma mark - 119: #pragma mark 响应屏幕即将旋转时的大事响应 120: - (void)willrotatescreenevent:(nsnot
16、ification *)notification 121: 122: nsvalue *framevalue = notification.userinfo valueforkey:uiapplicationstatusbarframeuserinfokey; 123: self rotatestatusbaranimatedwithframe:framevalue; 124: 125: 126: - (void)rotatestatusbaranimatedwithframe:(nsvalue *)framevalue 127: uiview animatewithduration:rota
17、tion_animation_duration animations: 128: self.alpha = 0; 129: completion:(bool finished) 130: self rotatestatusbarwithframe:framevalue; 131: uiview animatewithduration:rotation_animation_duration animations: 132: self.alpha = 1; 133: ; 134: ; 135: 136: 137: - (void)dealloc 138: 139: nsnotificationce
18、nter defaultcenter removeobserver:self; 140: textlabel release; 141: textlabel = nil; 142: 143: contentview release; 144: contentview = nil; 145: 146: super dealloc; 147: 148: 149: end 由于代码比较简洁,并且我在上述代码里有相应的说明,这里需要说明一点的是,默认我们继承自uiwindow的statusbaroverlay类是hidden状态,需要在初始化的时候设置它的hidden属性为no, 在屏幕旋转过程中,自定义的状态栏与uiviewcon
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度互联网企业派遣员工网络安全合同3篇
- 2025年全新公对公借款合同模板下载及服务支持10篇
- 二零二五年度体育馆租赁合同附体育赛事推广及赞助招商服务
- 2025版智能工厂生产线改造施工合同4篇
- 二零二五年度新能源产品销售代理合作合同范本3篇
- Bobath技术闫秀丽讲解
- 2025年度个人艺术品租赁借款合同范本及租赁期限约定
- 2025年室内墙面批白工程售后服务合同
- 二零二五年度户外广告照明外接电源供应合同
- 2025年度个人房屋抵押贷款担保及养老保障服务合同
- 道路沥青工程施工方案
- 2025年度正规离婚协议书电子版下载服务
- 《田口方法的导入》课件
- 内陆养殖与水产品市场营销策略考核试卷
- 电力电缆工程施工组织设计
- 医生给病人免责协议书(2篇)
- 票据业务居间合同模板
- 高中物理选择性必修2教材习题答案
- 应急预案评分标准表
- “网络安全课件:高校教师网络安全与信息化素养培训”
- 锂离子电池健康评估及剩余使用寿命预测方法研究
评论
0/150
提交评论