IOS[Swift版]常用操作代码片段

来源:互联网 发布:linux常用命令手册 编辑:程序博客网 时间:2024/06/03 13:24

设置状态栏背景颜色

func setStatusBarBackgroundColor(color : UIColor) {        let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView        let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView        /*         if statusBar.responds(to:Selector("setBackgroundColor:")) {         statusBar.backgroundColor = color         }*/        if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {            statusBar.backgroundColor = color        }    }

设置效果


设置状态栏字体颜色

状态字体颜色设置分为两种情况,当有页面有NavigationController,时需要这样进行设置:

self.navigationController?.navigationBar.barStyle = .blackTranslucent;

当没有NavigationController控件时,在ViewController中重写preferredStatusBarStyle属性进行设置

override var preferredStatusBarStyle: UIStatusBarStyle{get{ return .lightContent}};

效果如上图

提示框

let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .alert)alert.addAction(UIAlertAction.init(title: "确定", style:.default, handler: nil));alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil));// 显示弹出self.present(alert, animated: true) {   }

显示效果:
这里写图片描述

补充:
当把代码中

   let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .alert)   修改为    let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .actionSheet)

其它保持不变,显示效果如图
这里写图片描述

全部移除子控件方法

需要循环遍历子控件,并从父控件中移除

方法1

view.subviews.map({   $0.removeFromSuperview() })

方法2

for view in containerView.subviews{    view.removeFromSuperview()}

获取指定的storyboard

1.先创建一个storyboard,并命名为test.storyborad
2.拖一个view Controller到storyboard中,
并命名storyboard id=”test1”
3.代码如下

let storyboard=UIStoryboard(name: "test", bundle: nil);// 获取StoreBoard中指定的View Controllerlet viewTestController = storyboard.instantiateViewController(withIdentifier: "test1");// 跳转到viewTestController页面self.present(viewTestController, animated: true) {}
0 0
原创粉丝点击