奔五的人学iOS:swift对 状态栏、导航条若干技巧
来源:互联网 发布:知进退明得失的文章 编辑:程序博客网 时间:2024/05/16 09:22
1、状态栏反色
a.在info中添加 View controller-based status bar appearance,并将值设置 NO,表明不由系统控制,由vc自己控制;
b.在viewWillApear中使用以下代码实现反色
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)//UIApplication.shared.statusBarStyle = .lightContent
c.在viewWillDisapper中使用以下代码实现恢复
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default, animated: true)//UIApplication.shared.statusBarStyle = .default
两句效果一样,只是一种有动态效果,另外有人说override preferredStatusBarStyle,本人试了,不管用。
2、导航条透明以及文字颜色、其他按钮颜色
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white] self.navigationController?.navigationBar.tintColor = UIColor.white
以上代码在viewWillApear中,以下代码在viewWillDisappear中进行恢复
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] self.navigationController?.navigationBar.tintColor = UIColor.black
透明只对当前有效,返回上一级后不影响,文字颜色及控件颜色对上一级有影响,所以需要恢复
3、锁定屏幕方向
a.在AppDelegate中实现
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if !self.blockRotation { return UIInterfaceOrientationMask.all } else { return UIInterfaceOrientationMask.portrait } } var blockRotation: Bool = false
b.扩展UIViewController
extension UIViewController { var app : AppDelegate { return UIApplication.shared.delegate as! AppDelegate }}
c.在需要锁定屏幕的地方,一般是viewWillApear中
app.blockRotation = true old = UIDevice.current.value(forKey: "orientation") UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
d.退出时恢复状态
app.blockRotation = false
我这里是锁定为竖屏,其他值可根据你自己的情况选择
以上整理内容在xcode 8.2.1(8C1002)中,用swift3实现,希望对各位有帮助,不用东奔西跑了。
2 0
- 奔五的人学iOS:swift对 状态栏、导航条若干技巧
- 奔五的人学ios:swift居然没有字符串包含,找个简单的解决方法
- 奔五的人学iOS:swift获取网页并解析需要的内容(1)
- 奔五的人学IOS:swift练手与csdn,近期学习总结
- 奔五的人学IOS:Swift中的protocol及其应用实例
- iOS 11 Swift-导航&状态栏&tableview适配
- iOS状态栏 导航条 tabbar高度
- iOS隐藏状态栏、隐藏导航条
- iOS 9 隐藏导航条和状态栏
- iOS 9 隐藏导航条和状态栏
- 奔五的人学iOS:swift获取连接的wifi名称及ap的mac地址
- 奔五的人学iOS:用swift实现获取拼音首字母,支持取一句话中每字拼音首字母
- 半透明状态栏与导航条的错位
- 计算状态栏和导航条的高度
- 设置导航条和状态栏的颜色
- IOS 导航条 以及 状态栏颜色 相关设置
- swift设置导航条左边的按钮
- Swift加载带导航条的WebView
- Windows 10 IoT Serials 6
- 如何用MFC画出直线、虚线、折线、圆、椭圆、矩形、弧形(附上源码)
- 我的毕业设计(一):翻译工具箱(TranslationToolbox)
- 欢迎使用CSDN-markdown编辑器
- scikit-learn SVM支持向量机算法库使用小结
- 奔五的人学iOS:swift对 状态栏、导航条若干技巧
- Android分包MultiDex源码分析
- 【入门篇】Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布
- 高德地图定位功能(笔记)
- ffmpeg--问题
- Nginx配置文件nginx.conf中文详解
- 多线程3-同步及生命周期
- LAB颜色空间各通道的取值范围
- 2、函数的升级(上)