我正在尝试使用下面的代码在swift中将图像设置为导航栏的标题。代码构建成功,但图像不显示。任何建议将不胜感激。
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(contentsOfFile: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.navigationBar.titleView = self.convoImageView
}
}
答案 0 :(得分:1)
设置navigationItem的titleView。
let image = UIImage(named: "image_name.png")
self.navigationItem.titleView = UIImageView(image: image)
在您的代码中替换以下行:
self.convoImage = UIImage(contentsOfFile: "conversation.png")
与
self.convoImage = UIImage(named: "conversation.png")
答案 1 :(得分:0)
您应该设置convoImageView
框架。
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(named: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.convoImageView.frame = CGRectMake(0,0,720,100)
self.navigationBar.titleView = self.convoImageView
}
}