2016-07-20 158 views
3

我想要導航欄上的應用程序名稱後跟應用程序名稱。與此同時,應該有一個後退按鈕。Swift:導航欄上的徽標以及後退按鈕

下面是截圖:

Screenshot for question

我試過下面的代碼。

self.navigationItem.setHidesBackButton(false, animated:true); 

let imgLogo : UIImage = UIImage(named:"Logo")! 
let imgViewLogo : UIImageView = UIImageView(image: imgLogo) 
imgViewLogo.frame = CGRectMake(0, 0, 40, 40) 

let leftItem:UIBarButtonItem = UIBarButtonItem(customView: imgViewLogo) 
self.navigationItem.leftBarButtonItem = leftItem 
// App Name set on storyboard at design-time 

這會將徽標下圖所示的返回按鈕的上方。

Screenshot 2

如何才能實現這一目標?

回答

1

您可以使用navigationItem.titleView的自定義視圖。您的徽標和UIViewController的標題標籤創建的UIView,並設置
navigationItem.titleView = YOUR_CUSTOM_VIEW;

1

而不是使用.leftBarButtonItem使用:

let imgLogo : UIImage = UIImage(named:"Logo")! 
    let imgViewLogo : UIImageView = UIImageView(image: imgLogo) 
    imgViewLogo.frame = CGRectMake(0, 0, 40, 40) 

    let leftItem:UIBarButtonItem = UIBarButtonItem(customView: imgViewLogo) 
    self.navigationItem.leftBarButtonItems?.append(leftItem) 

它將與系統後退按鈕一起加入 enter image description here

+0

沒有工作:

在這個例子中,我還添加了一個空間,如果你需要爲中心以及您的標誌。它僅顯示後退按鈕和應用程序名稱。 –

0

你可以找到很多關於它的例子。

let leftButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "Logo")!, style: UIBarButtonItemStyle.Plain, target: self, action:#selector(MyViewController.leftButtonPress(_:))) 
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil) 
negativeSpacer.width = -5.0 // Left inset to better center your logo 
navigationItem.leftBarButtonItems = [negativeSpacer,leftButton] 

func leftButtonPress(sender: AnyObject?) { 
    // do whatever you want when you press back button 
} 
+0

仍然一樣。我想要後退按鈕,然後應用程序名稱是序列 –

+0

我不知道如果你看過leftBarButtonItems而不是leftBarButtonItem,它可以允許你添加多個元素;) –

+1

我已經檢查過兩個屬性了;-) –