3
A
回答
19
由於this answer shows,最簡單的解決方案是將文本圖像添加到您的形象,增加了導航欄,如下所示:
var image = UIImage(named: "logo.png")
self.navigationItem.titleView = UIImageView(image: image)
但是,如果你要加文本和圖像(例如就本地化而言),您可以將導航欄的標題視圖設置爲包含圖像和文本,方法是將它們添加到UIView
,並將navigationItem
的標題視圖設置爲UIView
,例如(假設導航欄是一個導航控制器的一部分):
// Only execute the code if there's a navigation controller
if self.navigationController == nil {
return
}
// Create a navView to add to the navigation bar
let navView = UIView()
// Create the label
let label = UILabel()
label.text = "Text"
label.sizeToFit()
label.center = navView.center
label.textAlignment = NSTextAlignment.Center
// Create the image view
let image = UIImageView()
image.image = UIImage(named: "Image.png")
// To maintain the image's aspect ratio:
let imageAspect = image.image!.size.width/image.image!.size.height
// Setting the image frame so that it's immediately before the text:
image.frame = CGRect(x: label.frame.origin.x-label.frame.size.height*imageAspect, y: label.frame.origin.y, width: label.frame.size.height*imageAspect, height: label.frame.size.height)
image.contentMode = UIViewContentMode.ScaleAspectFit
// Add both the label and image view to the navView
navView.addSubview(label)
navView.addSubview(image)
// Set the navigation bar's navigation item's titleView to the navView
self.navigationItem.titleView = navView
// Set the navView's frame to fit within the titleView
navView.sizeToFit()
1
這裏是我的雨燕4 2美分,因爲公認的答案並沒有爲我工作(主要是關閉屏幕):
// .. in ViewController
var navBar = CustomTitleView()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// =================== navBar =====================
navBar.loadWith(title: "Budget Overview", leftImage: Images.pie_chart)
self.navigationItem.titleView = navBar
}
class CustomTitleView: UIView
{
var title_label = CustomLabel()
var left_imageView = UIImageView()
override init(frame: CGRect){
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
setup()
}
func setup(){
self.addSubview(title_label)
self.addSubview(left_imageView)
}
func loadWith(title: String, leftImage: UIImage?)
{
//self.backgroundColor = .yellow
// =================== title_label ==================
//title_label.backgroundColor = .blue
title_label.text = title
title_label.font = UIFont.systemFont(ofSize: FontManager.fontSize + 5)
// =================== imageView ===================
left_imageView.image = leftImage
setupFrames()
}
func setupFrames()
{
let height: CGFloat = Navigation.topViewController()?.navigationController?.navigationBar.frame.height ?? 44
let image_size: CGFloat = height * 0.8
left_imageView.frame = CGRect(x: 0,
y: (height - image_size)/2,
width: (left_imageView.image == nil) ? 0 : image_size,
height: image_size)
let titleWidth: CGFloat = title_label.intrinsicContentSize.width + 10
title_label.frame = CGRect(x: left_imageView.frame.maxX + 5,
y: 0,
width: titleWidth,
height: height)
contentWidth = Int(left_imageView.frame.width)
self.frame = CGRect(x: 0, y: 0, width: CGFloat(contentWidth), height: height)
}
var contentWidth: Int = 0 //if its CGFloat, it infinitely calls layoutSubviews(), changing franction of a width
override func layoutSubviews() {
super.layoutSubviews()
self.frame.size.width = CGFloat(contentWidth)
}
}
相關問題
- 1. 在導航欄固定頂部之前添加標題圖片
- 2. 如何在導航欄中添加圖片以代替文字?
- 3. 在狀態欄和導航欄之間添加圖片
- 4. 如何在標籤欄的導航欄中添加文本字段和圖像?
- 5. iOS的11導航欄圖標問題
- 6. ios標題和字幕在導航欄中居中
- 7. 在左側導航欄上添加圖像和文字一起
- 8. iOS導航欄圖標問題
- 9. 如何在iPhone中一起添加圖片和文字導航欄
- 10. 使用Zurb的基礎在導航欄中添加圖片/圖標之前,在導航欄中添加圖片/圖標4
- 11. 子視圖添加到導航欄ios
- 12. 如何在反應導航標籤欄內添加圖片?
- 13. 導航欄有'圖標欄',但不添加鏈接從導航
- 14. 將標題和圖像添加到導航欄
- 15. 在導航欄中間添加徽標?
- 16. 在導航欄的後退按鈕和標題之間添加圖標
- 17. 將文本和進度圖標添加到導航欄
- 18. 導航欄圖片
- 19. 圖片導航欄
- 20. iOS:意外覆蓋導航欄的表格視圖標題欄
- 21. 如何添加一個標誌和文字在工具欄或導航欄
- 22. 在UICollectionView中添加導航欄在iOS 4中,iOS 11
- 23. 在ASP導航系統中添加文字而不是圖片
- 24. 添加圖片和標題
- 25. 如何在rmarkdown的導航欄標題欄中添加自定義徽標?
- 26. 返回導航標題欄 - 片段
- 27. iOS導航欄 - 我想在導航欄中用動態值設置標題
- 28. 在Bootstrap導航欄前添加徽標
- 29. iOS:導航欄標題 - 兩種顏色
- 30. 導航欄標題
完美答案,謝謝 –