【发布时间】:2016-11-24 08:34:40
【问题描述】:
我正在尝试使用静态图像以编程方式覆盖导航项的 rightButton 以实现此目的:
以下代码没有给出任何错误,但导航栏中没有显示任何内容。
import UIKit
import Foundation
class UICipNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Add BPT LOGO as UIBarButton
let logoBPT = UIImage(named: "Logo BPT")?.withRenderingMode(.alwaysOriginal)
let logoBPTBarButton = UIBarButtonItem(image: logoBPT, style: .plain, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = logoBPTBarButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
。
import UIKit
class UICipNavigationBar: UINavigationBar {
override init(frame: CGRect){
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.tintColor = UIColor.white
self.setBackgroundImage(UIImage.fromColor(color: UIColorFromHex(rgbValue: 0xffffff, alpha: 0.2)), for: UIBarMetrics.default)
self.shadowImage = UIImage()
self.isTranslucent = true
self.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Lato-Regular", size: 24)!]
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let newSize :CGSize = CGSize(width: self.superview!.bounds.size.width, height: 60)
return newSize
}
}
我也尝试检查视图,但似乎没有添加任何内容:
【问题讨论】:
-
试试这个 .. 可能对你有用stackoverflow.com/questions/30022780/…
-
刚试过,但我没有运气。代码总是覆盖情节提要吧?
-
您的代码运行良好。刚刚经过测试。可能是您的图像文件不存在?我不明白。为什么你的图片文件名中有空格
Logo BPT -
因为是我给图像集的名称,所以它有空间。除非我需要参考 -image 文件的名称 -。
-
检查我的更新.....
标签: ios swift xcode swift3 uibarbuttonitem