便捷方法库

2016-08-31  本文已影响9人  JaiUnChat

转载请注明!

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
上一篇下一篇

猜你喜欢

热点阅读