美文网首页
便捷方法库

便捷方法库

作者: JaiUnChat | 来源:发表于2016-08-31 10:06 被阅读9次

转载请注明!

1. 用十六进制初始化UIColor()

extension UIColor {

    func colorFrom(hex hex: Int) -> UIColor {
        let r = (hex & 0xff0000) >> 16
        let g = (hex & 0x00ff00) >> 8
        let b = hex & 0x0000ff
        return UIColor(red: CGFloat(r)/255, green: CGFloat(g)/255, blue:           CGFloat(b)/255, alpha: 1)  
    }
}

调用:

  let redColor = UIColor().colorFrom(hex: 0x123123)

2. NavigationBar 设置

具体参数解释参考另一篇文章

 func setNavigationControllerBar(navigationController: UINavigationController, barColor: UIColor, title: String, titleColor: UIColor = UIColor.whiteColor(), titleAttributes: [String: AnyObject] = [:]) {

    navigationController.navigationBar.barStyle = barStyle 
    navigationController.navigationBar.translucent = translcent // 注意会导致背景颜色变化, 详见解释
    navigationController.navigationBar.barTintColor = barColor
    navigationController.navigationBar.tintColor = titleColor
    navigationItem.title = title
    navigationController.navigationBar.titleTextAttributes = titleAttributes
    if navigationController.navigationBar.titleTextAttributes![NSForegroundColorAttributeName] == nil { // 逻辑判断什么小伙伴自己看看 不需要的就直接删除掉,就是在设置文字颜色的时候看看标题颜色是不是设置成了别的颜色
        navigationController.navigationBar.titleTextAttributes![NSForegroundColorAttributeName] = titleColor
    }

    
 }

调用:

override func viewDidLoad() {
    super.viewDidLoad()
    setNavigationControllerBar(self.navigationController!, barColor: UIColor.redColor(), title: "MainView",titleAttributes: [NSForegroundColorAttributeName: UIColor.purpleColor(), NSFontAttributeName: UIFont(name: "Times new roman", size: 20)!])
    setBackButtonItem(self.navigationItem, title: "Back")
 }

3. 导航栏返回按钮设置

func setBackButtonItem(navgationItem: UINavigationItem, title: String) {
   let backButton = UIBarButtonItem(title: title, style: .Plain, target: nil, action:  nil)
   navigationItem.backBarButtonItem = backButton

}

调用:

override func viewDidLoad() {
   super.viewDidLoad()
   setNavigationControllerBar(self.navigationController!, barColor: UIColor.redColor(), title: "MainView",titleAttributes: [NSForegroundColorAttributeName: UIColor.purpleColor(), NSFontAttributeName: UIFont(name: "Times new roman", size: 20)!])
   setBackButtonItem(self.navigationItem, title: "Back")
}

4. 网络图片

func getImage(fromURl url: URL) -> UIImage? {
    do {
        let imageData = try Data(contentsOf: url)
        return UIImage(data: imageData)
    }
    catch {
        print(error.localizedDescription)
        return nil
    }
}
// image.sd_setImage(with: url, placeholderImage: UIImage(named: "placeHolder")) SDWebImageView

相关文章

  • 便捷方法库

    转载请注明! 1. 用十六进制初始化UIColor() 调用: 2. NavigationBar 设置 具体参数解...

  • es6方法库

    Number.EPSILON =2^-52 (2.220446049250313e-16); 机器精度 Numb...

  • 思维方法库定律之一:人人都有思维方法库定律

    思维方法库定律之一:人人都有思维方法库定律每一个人都有一个思维方法库。没有哪一个人没有思维方法库,因为他天天要思维...

  • Java虚拟机概述

    运行时数据区,执行引擎,本地方法库,本地库接口 执行引擎:解释代码 本地方法库:系统底层方法库(C语言) Java...

  • build/make/core/Makefile:49: err

    集成第三方算法库时遇到的错误要求替换:/vendor/lib/hw/audio.stub.default.so[1...

  • 第一章《判决书》

    《判决书》 长青城临冬法库 刑事判决书 (赛路十七年)法库地字三十三号 诉讼机关:临冬法库 被告人:萨姆.克里艾,...

  • 第六章 狼口脱险

    第六章 狼口脱险 “一进法库门,一半牲口一半人;一出法库门,只见牲口不见人。” 这句老话的意思是,法库边门以外,是...

  • 法库樱花

  • Android调用第三方C++算法库

    背景 现在越来越多应用包含一些第三方C/C++算法库, 比如图像处理, 人脸检测, 语音识别等等. 第三方提供的算...

  • 外观模式

    兼容方式 小型属性样式方法库

网友评论

      本文标题:便捷方法库

      本文链接:https://www.haomeiwen.com/subject/keqfettx.html