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

    xiaoxiao2021-04-18  62

    设置状态栏背景颜色

    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 Controller let viewTestController = storyboard.instantiateViewController(withIdentifier: "test1"); // 跳转到viewTestController页面 self.present(viewTestController, animated: true) {}
    转载请注明原文地址: https://ju.6miu.com/read-674934.html

    最新回复(0)