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. iOS的11導航欄圖標問題
- 4. iOS導航欄圖標問題
- 5. 如何在iPhone中一起添加圖片和文字導航欄
- 6. 子視圖添加到導航欄ios
- 7. 如何在反應導航標籤欄內添加圖片?
- 8. 將標題和圖像添加到導航欄
- 9. 在導航欄中間添加徽標?
- 10. 在導航欄的後退按鈕和標題之間添加圖標
- 11. 導航欄圖片
- 12. 在UICollectionView中添加導航欄在iOS 4中,iOS 11
- 13. iOS:意外覆蓋導航欄的表格視圖標題欄
- 14. 如何添加一個標誌和文字在工具欄或導航欄
- 15. iOS導航欄 - 我想在導航欄中用動態值設置標題
- 16. 添加圖片和標題
- 17. 如何在rmarkdown的導航欄標題欄中添加自定義徽標?
- 18. 在Bootstrap導航欄前添加徽標
- 19. 在導航欄上的切換圖標旁邊添加徽標
- 20. 在引導欄的導航欄右側添加兩個圖標圖像
- 21. 導航欄中的頁面指示器和標題iOS Objective C
- 22. 添加導航欄
- 23. 導航欄標題字體大小
- 24. iOS導航欄圖標大小iPhone 6
- 25. 在xcode中爲導航欄添加圖標
- 26. iOS中的導航欄下的標籤
- 27. 在Xcode中添加導航欄控制器標題下的字幕
- 28. 將導航按鈕添加到統一標題欄/工具欄
- 29. 如何添加/使用默認的圖標導航欄
- 30. 添加導航抽屜通過替換在導航欄中的導航欄
完美答案,謝謝 –