在 swift 中,我没有将自定义导航栏与 AKSideMenu 一起使用
I don't use custom navbar with AKSideMenu in swift
我想使用我的自定义导航栏,我在板上进行了所有设置,但是当我 运行 我的应用程序排在第一位时,就像我转到不同页面时一样,标准导航栏是标准的设置。
我已经尝试使用代码添加导航栏
这个代码块是LeftMenuViewController
import UIKit
public class LeftMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView?
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: CGRect(x: 0, y: (self.view.frame.size.height - 54 * 5) / 2.0, width: self.view.frame.size.width, height: 54 * 5), style: .plain)
tableView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleWidth]
tableView.delegate = self
tableView.dataSource = self
tableView.isOpaque = false
tableView.backgroundColor = .clear
tableView.backgroundView = nil
tableView.separatorStyle = .none
tableView.bounces = false
self.tableView = tableView
self.view.addSubview(self.tableView!)
}
// MARK: - <UITableViewDelegate>
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.row {
case 0:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "haberlerVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 1:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "duyurularVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 2:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "firmalarVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 3:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "birimlerVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
default:
break
}
}
// MARK: - <UITableViewDataSource>
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection sectionIndex: Int) -> Int {
return 7
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "Cell"
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
cell!.backgroundColor = .clear
cell!.textLabel?.font = UIFont(name: "HelveticaNeue", size: 21)
cell!.textLabel?.textColor = .white
cell!.textLabel?.highlightedTextColor = .lightGray
cell!.selectedBackgroundView = UIView()
}
var titles = ["Haberler", "Duyurular", "Firmalar", "Yönetim Kurulu", "Bölge Müdürlüğü", "Hizmet Birimleri", "Öneri Şikayet"]
var images = ["haberler", "duyurular", "firmalar", "yonetim", "bolgemudurlugu", "birimler", "oneri" ]
cell!.textLabel?.text = titles[indexPath.row]
cell!.imageView?.image = UIImage(named: images[indexPath.row])
return cell!
}
}
//这个代码块是我的RootViewController
,故事板上没有任何导航栏
import UIKit
import AKSideMenu
public class RootViewController: AKSideMenu, AKSideMenuDelegate {
override public func awakeFromNib() {
super.awakeFromNib()
self.menuPreferredStatusBarStyle = .lightContent
self.contentViewShadowColor = .black
self.contentViewShadowOffset = CGSize(width: 0, height: 0)
self.contentViewShadowOpacity = 0.6
self.contentViewShadowRadius = 12
self.contentViewShadowEnabled = true
self.backgroundImage = UIImage(named: "menubg")
self.delegate = self
if let storyboard = self.storyboard {
self.contentViewController = storyboard.instantiateViewController(withIdentifier: "contentViewController")
self.leftMenuViewController = storyboard.instantiateViewController(withIdentifier: "leftMenuViewController")
}
}
// MARK: - <AKSideMenuDelegate>
public func sideMenu(_ sideMenu: AKSideMenu, willShowMenuViewController menuViewController: UIViewController) {
print("willShowMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, didShowMenuViewController menuViewController: UIViewController) {
print("didShowMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, willHideMenuViewController menuViewController: UIViewController) {
print("willHideMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, didHideMenuViewController menuViewController: UIViewController) {
print("didHideMenuViewController")
}
}
错误是视觉上的,
首先运行我的自定义导航栏
https://user-images.githubusercontent.com/13061013/59485353-3227bf80-8e7e-11e9-81d7-cec51bc328be.png
点击了不同的菜单页面或同一页面
https://user-images.githubusercontent.com/13061013/59485352-3227bf80-8e7e-11e9-832c-a18211167272.png
如果我没看错的话,你想在所有的 UIViewController 中都有自己的自定义导航栏。
打开您的 AppDelegate。swift 添加以下代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let color : UIColor = UIColor.white
let buttonFont : UIFont = UIFont.systemFont(ofSize: 14.0)
let titleFont : UIFont = UIFont.systemFont(ofSize: 18.0)
let attributes = [
NSAttributedStringKey.foregroundColor : color,
NSAttributedStringKey.font : buttonFont
]
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedStringKey.foregroundColor : color,
NSAttributedStringKey.font : titleFont
]
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().tintColor = UIColor.white
return true
}
我想使用我的自定义导航栏,我在板上进行了所有设置,但是当我 运行 我的应用程序排在第一位时,就像我转到不同页面时一样,标准导航栏是标准的设置。
我已经尝试使用代码添加导航栏
这个代码块是LeftMenuViewController
import UIKit
public class LeftMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView?
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: CGRect(x: 0, y: (self.view.frame.size.height - 54 * 5) / 2.0, width: self.view.frame.size.width, height: 54 * 5), style: .plain)
tableView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleWidth]
tableView.delegate = self
tableView.dataSource = self
tableView.isOpaque = false
tableView.backgroundColor = .clear
tableView.backgroundView = nil
tableView.separatorStyle = .none
tableView.bounces = false
self.tableView = tableView
self.view.addSubview(self.tableView!)
}
// MARK: - <UITableViewDelegate>
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.row {
case 0:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "haberlerVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 1:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "duyurularVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 2:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "firmalarVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
case 3:
self.sideMenuViewController!.setContentViewController(UINavigationController(rootViewController: self.storyboard!.instantiateViewController(withIdentifier: "birimlerVC")), animated: true)
self.sideMenuViewController!.hideMenuViewController()
default:
break
}
}
// MARK: - <UITableViewDataSource>
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection sectionIndex: Int) -> Int {
return 7
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "Cell"
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
cell!.backgroundColor = .clear
cell!.textLabel?.font = UIFont(name: "HelveticaNeue", size: 21)
cell!.textLabel?.textColor = .white
cell!.textLabel?.highlightedTextColor = .lightGray
cell!.selectedBackgroundView = UIView()
}
var titles = ["Haberler", "Duyurular", "Firmalar", "Yönetim Kurulu", "Bölge Müdürlüğü", "Hizmet Birimleri", "Öneri Şikayet"]
var images = ["haberler", "duyurular", "firmalar", "yonetim", "bolgemudurlugu", "birimler", "oneri" ]
cell!.textLabel?.text = titles[indexPath.row]
cell!.imageView?.image = UIImage(named: images[indexPath.row])
return cell!
}
}
//这个代码块是我的RootViewController
,故事板上没有任何导航栏
import UIKit
import AKSideMenu
public class RootViewController: AKSideMenu, AKSideMenuDelegate {
override public func awakeFromNib() {
super.awakeFromNib()
self.menuPreferredStatusBarStyle = .lightContent
self.contentViewShadowColor = .black
self.contentViewShadowOffset = CGSize(width: 0, height: 0)
self.contentViewShadowOpacity = 0.6
self.contentViewShadowRadius = 12
self.contentViewShadowEnabled = true
self.backgroundImage = UIImage(named: "menubg")
self.delegate = self
if let storyboard = self.storyboard {
self.contentViewController = storyboard.instantiateViewController(withIdentifier: "contentViewController")
self.leftMenuViewController = storyboard.instantiateViewController(withIdentifier: "leftMenuViewController")
}
}
// MARK: - <AKSideMenuDelegate>
public func sideMenu(_ sideMenu: AKSideMenu, willShowMenuViewController menuViewController: UIViewController) {
print("willShowMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, didShowMenuViewController menuViewController: UIViewController) {
print("didShowMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, willHideMenuViewController menuViewController: UIViewController) {
print("willHideMenuViewController")
}
public func sideMenu(_ sideMenu: AKSideMenu, didHideMenuViewController menuViewController: UIViewController) {
print("didHideMenuViewController")
}
}
错误是视觉上的,
首先运行我的自定义导航栏 https://user-images.githubusercontent.com/13061013/59485353-3227bf80-8e7e-11e9-81d7-cec51bc328be.png
点击了不同的菜单页面或同一页面 https://user-images.githubusercontent.com/13061013/59485352-3227bf80-8e7e-11e9-832c-a18211167272.png
如果我没看错的话,你想在所有的 UIViewController 中都有自己的自定义导航栏。
打开您的 AppDelegate。swift 添加以下代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let color : UIColor = UIColor.white
let buttonFont : UIFont = UIFont.systemFont(ofSize: 14.0)
let titleFont : UIFont = UIFont.systemFont(ofSize: 18.0)
let attributes = [
NSAttributedStringKey.foregroundColor : color,
NSAttributedStringKey.font : buttonFont
]
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedStringKey.foregroundColor : color,
NSAttributedStringKey.font : titleFont
]
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().tintColor = UIColor.white
return true
}