如何在IOS 11中为新的大型条设置渐变颜色的UINavigationbar?

时间:2017-12-22 14:02:03

标签: ios uinavigationcontroller uinavigationbar calayer

我需要用渐变色制作这个条。我已经在普通导航栏中找到了这个问题,但它不适合新的大型导航栏。

enter image description here

1 个答案:

答案 0 :(得分:0)

要设置背景颜色,请使用此代码

  protocol Presentable {
  associatedtype View: ViewAble
  weak var view: View? {get set}

  init(with view: View)

  func onAttach(view: View)
  func onDetach()

   var isAttached: Bool {get}
}

extension Presentable {
  var isAttached: Bool {
    return view != nil
  }
}



class Presenter: Presentable {

 weak var view: ViewAble? {
    didSet {
      if let view = view {
        onAttach(view: view)
      } else {
        onDetach()
      }
    }
  }


  required init(with view: ViewAble) {
    self.view = view
  }

  func onAttach(view: View) {
    //pre set up on construction

  }
  func onDetach() {
    //release some resource on destroying view
  }
}




@objc protocol ViewAble: class {
  @objc optional func showError(_ message: String, _ callBack: (() -> Void)?)
}

extension ViewAble where Self: UIViewController {
  func showAlert(_ message: String?, _ callBack: (() -> Void)? = nil) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default) { action in
      callBack?()
    })
    self.present(alertController, animated: true, completion: callBack)
  }

  func showLoading() {
    //show default Loading here and override if want custom

  }
  func stopLoading() {
    //stop default Loading
  }
}

class ViewController: ViewAble {

 }

要更改文字和字体,请使用此代码

self.navigationController?.setBackgroundColor(UIColor.blue)

试试这个